Paul Spicer
2013-03-20 16:21:41 UTC
I have a question for those of you who are skilled in the ways of bash
commands.
Let me first set up the scenario. I have a config file that has a bunch of
setting in it that need to be changed all at once.
For example:
#Config file
[main]
setting1 value1
setting2 value2
setting3 value3
[users]
user1 password
user2 password
user3 password
...
user50 password
(All of the users have the same password of "password.")
What I'm trying to accomplish is changing all instances of 'password' to
that particular user's username so the end result would look like:
#Config file
[main]
setting1 value1
setting2 value2
setting3 value3
[users]
user1 user1
user2 user2
user3 user3
...
user50 user50
Normally, if I'm changing something simple like this, I'd just use sed.
However, since I want to change the 'password' to the username, I figure
I'd have to use awk to grab the name first and then place it into the sed
command. Something like:
$ cat config.file | sed "s/password/`awk '{print $1}'`/g" > newconfig.file
But that doesn't exactly work... At all, really.
I ended up building an actual .sh script that DID accomplish what I was
trying to do, but I want to know if it's possible to do it in a single line
like I wanted to do in the first place.
commands.
Let me first set up the scenario. I have a config file that has a bunch of
setting in it that need to be changed all at once.
For example:
#Config file
[main]
setting1 value1
setting2 value2
setting3 value3
[users]
user1 password
user2 password
user3 password
...
user50 password
(All of the users have the same password of "password.")
What I'm trying to accomplish is changing all instances of 'password' to
that particular user's username so the end result would look like:
#Config file
[main]
setting1 value1
setting2 value2
setting3 value3
[users]
user1 user1
user2 user2
user3 user3
...
user50 user50
Normally, if I'm changing something simple like this, I'd just use sed.
However, since I want to change the 'password' to the username, I figure
I'd have to use awk to grab the name first and then place it into the sed
command. Something like:
$ cat config.file | sed "s/password/`awk '{print $1}'`/g" > newconfig.file
But that doesn't exactly work... At all, really.
I ended up building an actual .sh script that DID accomplish what I was
trying to do, but I want to know if it's possible to do it in a single line
like I wanted to do in the first place.