LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-27-2015, 11:14 AM   #1
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,420
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
Question sed not deleting entries in a file


I have a script that prepares a RHEL VM to become a template in vSphere. Part of the script purges out old ip addresses out of /etc/hosts. I'm looking to delete out anything with the 1st octet of 143 or 192.

I'm trying to use the following from my script and from the CLI and its not deleting out those entries:

Code:
sed '/143/d' /etc/hosts
sed '/192/d' /etc/hosts
or

Code:
 
sed -i ".bak" '/143/d' /etc/hosts
sed -i ".bak" '/192/d' /etc/hosts
Neither is working because when I run the following: cat /etc/hosts

Those ip addresses are still there.

Any ideas?

Last edited by JockVSJock; 10-27-2015 at 11:15 AM.
 
Old 10-27-2015, 11:17 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
the first two prints the output to stdout, will not modify the original file.
I think in the second case there can be no space in between -i and ".bak" and therefore it will take .bak as sed script and /143/d as a filename.

(finally I think you need to run it as root)
 
Old 10-27-2015, 12:49 PM   #3
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,420

Original Poster
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
I'm running the script as root.

I guess I wasn't aware that sed sends it output to the screen. Its weird in that I run the sed command, and I see the changes, however if I check the file, those IP addresses are still there in /etc/hosts.

Found that the following flag stops output from going to the console:

Code:
-n
However doing some more reading, this is a bash script, thus my first line:

Code:
#!/bin/bash
Will there be a conflict with having sed commands in a bash script?
 
Old 10-27-2015, 01:16 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
When you "simply" enter a sed-command, it's also happening inside a bash-shell.
 
Old 10-27-2015, 01:25 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You can use any CLI command, like sed, in your script.

Did you follow the advice that there is to be no space between -i and ".bak", btw the quotes are not required.
So your command should be:
Code:
sed -r -i.bak '/143|192/d' /etc/hosts
May I also suggest you use this with extreme caution. You said:
Quote:
I'm looking to delete out anything with the 1st octet of 143 or 192.
However, the above will delete and line with those numbers anywhere on the line, so it may delete the following:
Code:
192.168.1.1
10.192.1.254
# I have 143 things to write about
...
As you can see, some of the above you may want to keep as your statement says you should only delete the first of those entries
 
Old 10-28-2015, 12:05 PM   #6
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,420

Original Poster
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
I tried what was recommended above and it worked for /etc/hosts. For the final code quote, I'm still trying to use sed and awk with my sys admin work daily, which I find hard to do. I understand that if I were to have

Code:
10.143.10.1
It would remove that, so I still need to understand how to have the sed only look at the 1st octet only.

However I'm trying to use sed to automate commenting out certain values in a file.

For example:
Code:
sed -n -i.bak 's/HWADDR/#HWADDR/' /etc/sysconfig/networking/profiles/default/ifcfg-eth0
sed -n -i.bak 's/IPADDR/#IPADDR/' /etc/sysconfig/networking/profiles/default/ifcfg-eth0
sed -n -i.bak 's/GATEWAY/#GATEWAY/' /etc/sysconfig/networking/profiles/default/ifcfg-eth0
sed -n -i.bak 's/USERCTL/#USERCTL/' /etc/sysconfig/networking/profiles/default/ifcfg-eth0
Deletes the old file, creates a new ifcfg-eth0.bak and still doesnt' comment out the values in the sed statement.

I'm still not understanding this.
 
Old 10-28-2015, 01:03 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You will need to know how the lines are configured to make sure you only look at the first octet, eg. if the address always starts at the beginning of the line you could do:
Code:
sed -i.bak '/^143\./d' file
Here we have added the anchor ^ to say only look at lines where 143 is at the start of the line and then that it is followed by a period (which needs to be escaped as on its own it has special meaning)

As for your new problem, some of your information is a little off. The original file is not deleted. A backup is made with the new additional extension of .bak and the original file
is edited in place (-i). Again you need to know the configuration and layout of the file you are changing as currently all your calls to sed will add a hash (#) in front of the string irrelevant
of where it is on the line. This can cause problems as sometimes a # might only make a line commented if it appears at the start of the line (this may or may not be the case for the file you are changing).

Please confirm that after the call to sed you are looking at the file /etc/sysconfig/networking/profiles/default/ifcfg-eth0?
 
Old 10-28-2015, 02:52 PM   #8
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,420

Original Poster
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
Quote:
Originally Posted by grail View Post

Please confirm that after the call to sed you are looking at the file /etc/sysconfig/networking/profiles/default/ifcfg-eth0?
Yes, that is correct the file that I'm wanting to modify is /etc/sysconfig/networking/profiles/default/ifcfg-eth0

However...I think I got it...this is the sed I'm using:

Code:
sed -i.bak '/HWADDR/s/^/#/' /etc/sysconfig/networking/profiles/default/ifcfg-eth0
sed -i.bak '/IPADDR/s/^/#/' /etc/sysconfig/networking/profiles/default/ifcfg-eth0
sed -i.bak '/GATEWAY/s/^/#/' /etc/sysconfig/networking/profiles/default/ifcfg-eth0
sed -i.bak '/USERCTL/s/^/#/' /etc/sysconfig/networking/profiles/default/ifcfg-eth0
I tested this on a test server and it worked.

I'm just trying to get over the hump of sed/awk and trying to use it in my everyday admin work.

thanks
 
Old 10-29-2015, 12:33 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Great work Glad you got there in the end.

As with previous example, you can place your 4 changes in 1 sed command as well:
Code:
sed -r -i.bak '/HWADDR|IPADDR|GATEWAY|USERCTL/s/^/#/' file

# or a little more convoluted
sed -r -i.bak '/(HW|IP)ADDR|GATEWAY|USERCTL/s/^/#/' file
Here are some references you should keep bookmarked:

http://www.gnu.org/software/gawk/man...ode/index.html
http://www.grymoire.com/Unix/Sed.html
 
  


Reply

Tags
sed bash



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] deleting periods using sed nourah Programming 10 08-23-2011 11:32 AM
Searching and deleting using Sed gregarion Programming 9 01-10-2010 07:51 AM
deleting unique HCL entries pbhj LQ Suggestions & Feedback 7 05-24-2008 07:46 AM
deleting entries in routing table mwagz Linux - Networking 1 01-30-2007 01:13 PM
how to delete duplicates entries in xml file using sed/awk/sort ? catzilla Linux - Software 1 10-28-2005 02:57 PM

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

All times are GMT -5. The time now is 12:07 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