LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   sed trick to do conditional append (https://www.linuxquestions.org/questions/linux-general-1/sed-trick-to-do-conditional-append-925356/)

the_gripmaster 01-23-2012 08:18 PM

sed trick to do conditional append
 
Existing file

Code:

root:x:0:
wheel:x:10:user1,user2
mail:x:12:mail

Desired file

Code:

root:x:0:
wheel:x:10:user1,user2,user3
mail:x:12:mail

What I want to do is use sed to append user3 at the end of the wheel group.

jhwilliams 01-23-2012 09:14 PM

Code:

sed 's/^\(wheel:.*\)/\1,user3/' /etc/group

the_gripmaster 01-23-2012 09:38 PM

Quote:

Originally Posted by jhwilliams (Post 4582687)
Code:

sed 's/^\(wheel:.*\)/\1,user3/' /etc/group

Thanks.

jschiwal 01-23-2012 11:47 PM

I'd recommend changing it slightly, using the pattern /wheel:/ to select the line to edit.
Code:

/^wheel:/s/.*/&,user3/
It only edits lines starting with "wheel".

Dark_Helmet 01-23-2012 11:58 PM

And a slight modification to jschiwal's pattern ;)

Code:

/^wheel:/s/\(.*\)/\1,user3/
EDIT:
Just to clarify, this form and jhwilliams's are the same thing--just formatted differently (as far as I can tell)

jschiwal 01-24-2012 05:14 AM

Thanks Dark Helmet. I modified my post. Didn't type the ampersand. I get so caught up avoiding auto-discorrection, I end up making dumb omissions.

the_gripmaster 01-24-2012 05:17 AM

Why is sed so cryptic?

jhwilliams 01-24-2012 07:53 AM

Quote:

Originally Posted by the_gripmaster (Post 4582996)
Why is sed so cryptic?

I remember feeling that way, too. Here, this is a /little/ clearer, with extended regex:

Code:

sed -r 's/(wheel:.*)/\1,user3/' /etc/group
Regex is a powerful tool - it's a trade off between ease of use and capability of expression. I think if we tried to create such a regular expression language of our own it might end up being just as ugly, if only different.

Incidentally, if I am correctly guessing what you're trying to do, you might find it easier to just add the group membership to the user's list with this command:

Code:

sudo usermod -a -G wheel user3

trey85stang 01-24-2012 09:30 AM

Quote:

Originally Posted by the_gripmaster (Post 4582996)
Why is sed so cryptic?

It's not really cryptic, its just a language you don't understand (yet). Basically as said above the language you do not understand is regular expressions... (also the shell escapes get confusing but come second nature when you realize they are shell escapes) Here is a good getting started tutorial: http://gnosis.cx/publish/programming...pressions.html

You don't have to get too advanced to get a good grasp and be able to start using them, also regular expressions are supported in many languages and commands so knowing a little bit comes in handy.


Quote:

Originally Posted by Dark_Helmet (Post 4582762)
And a slight modification to jschiwal's pattern ;)

Code:

/^wheel:/s/\(.*\)/\1,user3/
EDIT:
Just to clarify, this form and jhwilliams's are the same thing--just formatted differently (as far as I can tell)

the above sed line will not work correctly if there is no user in the wheel group... (the sed line will work. the group entry will not.)

This should work though...
Code:

sed "/^wheel:/s/\(.*\)/\1,user3/;s/:,/:/" /etc/group

the_gripmaster 01-24-2012 05:39 PM

Quote:

Originally Posted by jhwilliams (Post 4583083)

Incidentally, if I am correctly guessing what you're trying to do, you might find it easier to just add the group membership to the user's list with this command:

Code:

sudo usermod -a -G wheel user3

Excellent! I wonder why I never thought about adding users to the wheel group this way.


All times are GMT -5. The time now is 10:40 AM.