LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 11-23-2012, 11:04 AM   #1
zulkifal
LQ Newbie
 
Registered: Jul 2012
Posts: 22

Rep: Reputation: Disabled
sed command search and replace


I have this problem where I am looking for a string and replacing it but the issue is that my search criteria returns multiple strings and i need to change only one e.g in /etc/sudoers file

#wheel......
#whel...... NO PASSWD

i only want to remove hash from the line that does not end with "NO PASSWD".I tried using line number method but this string seems to have a different line numbering on different systems. Any ideas?
Thanks
 
Old 11-23-2012, 11:48 AM   #2
steelneck
Member
 
Registered: Nov 2005
Distribution: Slackware, Arch
Posts: 43

Rep: Reputation: 8
You could pipe the inverted output from grep to sed, like this:

Code:
grep -v "NO PASS" /etc/sudoers | sed 's/#wheel/wheel/'
You may also know that you can use the -i option for sed to write the change to file instantly.
 
Old 11-23-2012, 12:27 PM   #3
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 870
Blog Entries: 2

Rep: Reputation: 219Reputation: 219Reputation: 219
I'd advise against changing sudoers directly. Always write to a new file such as sudoers.tmp (with correct owner, group and mode) then use
Code:
visudo -c sudoers.tmp
to confirm satisfactory syntax. Finally rename the temp file to sudoers.
 
Old 11-23-2012, 01:04 PM   #4
zulkifal
LQ Newbie
 
Registered: Jul 2012
Posts: 22

Original Poster
Rep: Reputation: Disabled
I agree with you "linosaurusroot".
Thanks "steelneck" for reply but that command doesnt seem to work. lets make it this way:
what is the right command to search for a string:
# %wheel ALL=(ALL) ALL

and replace it with

%wheel ALL=(ALL) ALL

keep in mind that we have a similar string like
# %wheel ALL=(ALL) NOPASSWD: ALL
in the same file
Thanks
 
Old 11-23-2012, 01:24 PM   #5
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 870
Blog Entries: 2

Rep: Reputation: 219Reputation: 219Reputation: 219
You could cheat by having for each block of rules you are preparing to switch on and off surround them by comments:
Code:
groucho ALL=/bin/false
# begin wheel group all access
%wheel ALL=(ALL) ALL
# end wheel group all access
harpo ALL=/bin/true
wax on
Code:
sed -i '/# begin wheel/,/# end wheel/s/^#%\(.*\)/%\1/'   sudoers.tmp
wax off
Code:
 sed -i '/# begin wheel/,/# end wheel/s/^\(%wheel\)/#\1/'  sudoers.tmp
 
Old 11-24-2012, 11:19 AM   #6
steelneck
Member
 
Registered: Nov 2005
Distribution: Slackware, Arch
Posts: 43

Rep: Reputation: 8
Quote:
Originally Posted by zulkifal View Post
Thanks "steelneck" for reply but that command doesnt seem to work.
Well, in first post you wrote "NO PASSWD" and now it is "NOPASSWD", could that be the root to that my line did not work for you?

As to the question you wrote now:

Code:
sed 's/# %wheel ALL=(ALL) ALL/%wheel ALL=(ALL) ALL/'
That one will not hit the line containing NOPASSWD
 
1 members found this post helpful.
Old 11-26-2012, 09:19 AM   #7
zulkifal
LQ Newbie
 
Registered: Jul 2012
Posts: 22

Original Poster
Rep: Reputation: Disabled
Thx steelneck , by the way "linosaurusroot" . can you explain what you wrote? bcause i didnt get it.
 
Old 11-26-2012, 11:01 AM   #8
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,210

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
this worx for me:
Code:
 [schneidz@hyper ~]$ sed s/'# %wheel ALL=(ALL) ALL'/'%wheel ALL=(ALL) ALL'/ sudo.tmp
%wheel ALL=(ALL) ALL
 
Old 11-26-2012, 11:56 AM   #9
zulkifal
LQ Newbie
 
Registered: Jul 2012
Posts: 22

Original Poster
Rep: Reputation: Disabled
Its working now . Thanks everyone
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using sed - search and replace seebee Programming 5 06-07-2011 10:47 PM
[SOLVED] Using sed to search and replace backwards jimieee Programming 15 10-25-2010 11:13 AM
sed search replace tomerbd1 Linux - General 9 04-10-2008 05:31 AM
sed question for search and replace jakev383 Linux - General 8 05-05-2007 06:40 AM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 02:04 AM


All times are GMT -5. The time now is 10:13 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration