LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-16-2009, 05:33 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
quick sed question - how do you add a line after a match?


I need to edit the sudo file on lots of machines. I figure the easiest way is to run a for loop to ssh to each box, sed the sudoers file to a new file & copy/move back over the top of the original (and of course, change permissions accordingly)

The problem is, I'm not sure how to add a line after a match with sed.

So if my file looks like:

Code:
# comments

root    ALL=(ALL) ALL
briank  ALL=(ALL) ALL
other   ALL=(ALL) ALL

# more comments
I'd like it to add the new sudoer after "other" so the file looks like:

Code:
# comments

root    ALL=(ALL) ALL
briank  ALL=(ALL) ALL
other   ALL=(ALL) ALL
newguy  ALL=(ALL) ALL

# more comments
I'm open to other solutions if this doesn't seem reasonable (other solutions that result in one new user having sudo privs - not everyone in a group or something like that. )
 
Old 09-16-2009, 06:36 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You simply need to match the pattern of the 'other' line and add the new line:
Code:
sudo sed -i '/^other/a\
newguy\tALL=(ALL) ALL' /etc/sudoers

Last edited by jschiwal; 09-16-2009 at 06:39 PM.
 
Old 09-16-2009, 07:22 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
by "adding", the basic mechanism can be just printing . eg
Code:
awk '/^other/{print;print "new value";next}1' file > temp; mv temp file
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert line on match only once with sed? lhouk Programming 38 08-10-2010 09:23 AM
Perl question: delete line from text file with duplicate match at beginning of line mrealty Programming 7 04-01-2009 06:46 PM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 08:55 AM
sed display line after pattern match inonzi_prowler Linux - Software 3 02-19-2007 01:47 PM
Using sed to add carriage retuns and line numbers. Optimistic Programming 11 04-08-2005 01:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:36 AM.

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