LinuxQuestions.org
Help answer threads with 0 replies.
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 04-04-2017, 02:19 AM   #1
tara
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Centos 7
Posts: 58

Rep: Reputation: 15
Remove matching line from file


Hi,

I have the following line put in the /etc/sudoers in my kickstart file which after an action following installation is performed i wish to have the line removed.
Code:
echo "system  ALL=NOPASSWD: ALL" >> /etc/sudoers
I have tried a few different sed commands but I can't get any of them to work, not sure if it is because there is whitespace in the line or not.... The sed (or equivalent command needs to run in a script)


Any ideas would be greatly appreciated.

Thanks!

Last edited by tara; 04-04-2017 at 02:22 AM.
 
Old 04-04-2017, 02:26 AM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,428
Blog Entries: 3

Rep: Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205
Quote:
Originally Posted by tara View Post
I have tried a few different sed commands but I can't get any of them to work, not sure if it is because there is whitespace in the line or not....
But the pattern you use should then take into account the possibility that there might be white space. Can you show what you have tried so far?

Also, which version of sed do you have and does it support the -i option?

Last edited by Turbocapitalist; 04-04-2017 at 02:27 AM.
 
Old 04-04-2017, 02:30 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,576

Rep: Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341
yes, how could you expect any fix/explanation if you won't show us what/how did you try?
 
Old 04-04-2017, 02:33 AM   #4
tara
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Centos 7
Posts: 58

Original Poster
Rep: Reputation: 15
sed is version 4.2.2 and yes it supports -i

Does the whitespace have to match perfectly? i.e. 2 spaces, 3 spaces etc?

I have tried
sed -i '/ALL=NOPASSWD/d' /etc/sudoers

as well as putting in the entire line and neither option works.

I have also tried using '$d' to just delete the last line (which is not a nice why of doing it) but when it goes from my kickstart into my bash_profile the $d disappears....
 
Old 04-04-2017, 02:34 AM   #5
tara
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Centos 7
Posts: 58

Original Poster
Rep: Reputation: 15
I'm happy to use grep or awk, just don't know which might be the best way...
 
Old 04-04-2017, 02:37 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,576

Rep: Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341
you can try it without -i, just to see what's happening:
sed '/ALL=NOPASSWD/d' /etc/sudoers
actually that should work, but it will remove all the lines containing your pattern.
white space and others are irrelevant here and now, you need to check only if that pattern matches.
 
  


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
[SOLVED] sed command for matching the end character of a line and replacing the whole line. mamunm Linux - Newbie 4 04-12-2014 01:19 AM
[SOLVED] awk with pipe delimited file (specific column matching and multiple pattern matching) lolmon Programming 4 08-31-2011 01:17 PM
Delete line in file matching string Black Sun Programming 7 01-28-2010 04:18 AM
PERL:: trying to remove last line of file ocularbob Programming 16 09-01-2003 02:07 PM
How to remove line of text from file netkepala Linux - General 2 05-23-2003 12:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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