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! |
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.
|
sed can do that for you quite easily.
e.g. Code:
gazl@nix:/tmp$ cat testing |
I found a 1 liner googling that will do exactly what you want.
Code:
awk '/search string here/{c=2}!(c&&c--)' file >newfile I created a file called TEST with the following contents. Quote:
Code:
awk '/car drove/{c=3}!(c&&c--)' TEST >RESULTS Quote:
|
Marvelous; thank you sir. Your google skills are strong
|
All times are GMT -5. The time now is 07:31 AM. |