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 |
You could pipe the inverted output from grep to sed, like this:
Code:
grep -v "NO PASS" /etc/sudoers | sed 's/#wheel/wheel/' |
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 |
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 |
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 Code:
sed -i '/# begin wheel/,/# end wheel/s/^#%\(.*\)/%\1/' sudoers.tmp Code:
sed -i '/# begin wheel/,/# end wheel/s/^\(%wheel\)/#\1/' sudoers.tmp |
Quote:
As to the question you wrote now: Code:
sed 's/# %wheel ALL=(ALL) ALL/%wheel ALL=(ALL) ALL/' |
Thx steelneck , by the way "linosaurusroot" . can you explain what you wrote? bcause i didnt get it.
|
this worx for me:
Code:
[schneidz@hyper ~]$ sed s/'# %wheel ALL=(ALL) ALL'/'%wheel ALL=(ALL) ALL'/ sudo.tmp |
Its working now . Thanks everyone
|
All times are GMT -5. The time now is 04:40 AM. |