remove part of a text file based on a string pattern
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.
remove part of a text file based on a string pattern
Hi there,
I want a bash script that removes part of a text file based on a specific pattern.
For example let's take the dhcpd.conf that contains many host containers like this:
I want my script to take the MAC as argument and remove that host.
Like: ./remome_host 01:23:47AC:99:14
and it removes:
host dellxx {
hardware ethernet 01:23:47AC:99:14;
fixed-address 10.0.0.1;
Great. In sed and awk, you don't get much flexibility so don't try to make a generic dhcpd.conf parser. Just make one for your use case. If you need something bigger, fancier, or more complex then escalate to perl and try Net::ISC::DHCPd
I would add that this is doable in bash, but awk and others are easier and probably preferable based on the task
Like many others, please remember to include a proper example, ie. a file that has both what needs to be removed and what needs to be kept.
Also include the output of the operation so it is unambiguous as to your need.
Lastly, please use [code][/code] tags around both code and example data.
in awk it looks quite simple:
the "usual" way is to set the RS to a keyword, like \nhost<space> (or something similar), and now you can skip "lines" containing the given filter.
The solution from MadeInGermany works great. I dind't understand everything, but I am on my way..
Also the first solution from szboardstretcher is ok. It returns the searched string, I still have to remove that from the initial file.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.