[SOLVED] Delete all lines containing a string, plus 4 lines below it?
Linux - NewbieThis 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
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.
Delete all lines containing a string, plus 4 lines below it?
Hi everyone,
I've come across an unusual requirement for a service in my Ubuntu system.
Simply put, I need to find a way to search for all instances of a term in a file, delete lines containing containing that term, and delete four lines below each instance of that term.
Either that, or copy the entirety of a file to a new file and skip over all lines containing the term plus four below it.
This sounds kinda weird, I know. Without going too far into detail, I either have to change the logfile format for a server I'm running which is a huge pain in the butt, or I can just run a script to edit an HTML report generated from said logs. (Said report is really just for managers to peruse, and I like my log format, so I'm pursuing option 2.)
Thanks in advance!
Last edited by RedHelix; 01-26-2010 at 03:51 PM.
Click here to see the post LQ members have rated as the most helpful post in this thread.
Well this is a great thread for me to follow since I am working on my Scripting skills. I know how to find the string in a script, but figuring out how to delete the line and the next 4 lines I do not know how to do. Sounds like using sed or awk might be a good solution here.
You can replace search string here with your search string. c= to the number of lines you want to delete. file is the name of the file to search and newfile is the name of the file that will contained the results.
I created a file called TEST with the following contents.
Quote:
the quick brown fox jumped over the fence
a car drove through a bush
trees are healthy
meat is good
this is some random text
I want to search TEST for the string "car drove" and delete that line and 2 lines below it and save the results to a file called RESULTS. I run this command
Code:
awk '/car drove/{c=3}!(c&&c--)' TEST >RESULTS
The outcome is the file RESULTS contains the following text
Quote:
the quick brown fox jumped over the fence
this is some random text
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.