LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-23-2012, 09:18 PM   #1
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 364

Rep: Reputation: 38
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.
 
Old 01-23-2012, 10:14 PM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
Code:
sed 's/^\(wheel:.*\)/\1,user3/' /etc/group
 
1 members found this post helpful.
Old 01-23-2012, 10:38 PM   #3
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 364

Original Poster
Rep: Reputation: 38
Quote:
Originally Posted by jhwilliams View Post
Code:
sed 's/^\(wheel:.*\)/\1,user3/' /etc/group
Thanks.
 
Old 01-24-2012, 12:47 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 679Reputation: 679Reputation: 679Reputation: 679Reputation: 679Reputation: 679
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".

Last edited by jschiwal; 01-24-2012 at 06:14 AM.
 
Old 01-24-2012, 12:58 AM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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)

Last edited by Dark_Helmet; 01-24-2012 at 01:01 AM.
 
Old 01-24-2012, 06:14 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 679Reputation: 679Reputation: 679Reputation: 679Reputation: 679Reputation: 679
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.
 
Old 01-24-2012, 06:17 AM   #7
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 364

Original Poster
Rep: Reputation: 38
Question

Why is sed so cryptic?
 
Old 01-24-2012, 08:53 AM   #8
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
Quote:
Originally Posted by the_gripmaster View Post
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
 
1 members found this post helpful.
Old 01-24-2012, 10:30 AM   #9
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,091

Rep: Reputation: 41
Quote:
Originally Posted by the_gripmaster View Post
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 View Post
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
 
Old 01-24-2012, 06:39 PM   #10
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 364

Original Poster
Rep: Reputation: 38
Quote:
Originally Posted by jhwilliams View Post

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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed or awk conditional string replacement tsdjim Linux - Newbie 3 01-23-2012 01:14 PM
[SOLVED] sed -- conditional catenation of lines danielbmartin Programming 6 01-20-2012 05:05 AM
SED script with conditional filtering ut0ugh1 Programming 4 11-08-2011 02:13 PM
Help: Sed trick the_gripmaster Linux - General 2 07-19-2009 11:03 AM
sed substitution conditional frostillicus Linux - Newbie 3 04-17-2005 01:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration