Discussion:
sed and awk; can this be done?
Paul Spicer
2013-03-20 16:21:41 UTC
Permalink
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.
Steve Litt
2013-03-21 14:05:35 UTC
Permalink
On Wed, 20 Mar 2013 12:21:41 -0400
Post by Paul Spicer
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.
#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'
#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
$ 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.
It's trivial if you're sure the word "password" will occur only in
[users]. It would be something like (untested)

cat orgfile | sed -e "s/{.*}password/\1/" > newfile

Personally I'd just use an awk script. Awk is the perfect tool to
replace only stuff in the [users] section. Here's some stuff I wrote on
awk:

http://www.troubleshooters.com/codecorn/awk/index.htm

SteveT

---------------------------------------------------------------------
Archive http://marc.info/?l=jaxlug-list&r=1&w=2
RSS Feed http://www.mail-archive.com/list-QVYiWngmsdMdnm+***@public.gmane.org/maillist.xml
Unsubscribe list-unsubscribe-QVYiWngmsdMdnm+***@public.gmane.org
Loading...