Re: sed and awk; can this be done?

Steve Litt <[email protected]> Thu, 21 Mar 2013 10:05:35 -0400
Newsgroups gmane.org.user-groups.jaxlug
Message-ID <20130321100535.47308648@mylap3>
On Wed, 20 Mar 2013 12:21:41 -0400
Paul Spicer <[email protected]> wrote:

> 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.

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/[email protected]/maillist.xml
Unsubscribe  [email protected]