ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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?
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.
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.