LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
LinkBack Search this Thread
Old 01-26-2010, 03:48 PM   #1
RedHelix
LQ Newbie
 
Registered: Mar 2004
Location: BAWstun Massachusetts
Distribution: Red Hat
Posts: 9

Rep: Reputation: 0
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.
Old 01-26-2010, 03:52 PM   #2
worm5252
Member
 
Registered: Oct 2004
Location: Atlanta
Distribution: CentOS, RHEL, HP-UX
Posts: 565

Rep: Reputation: 57
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.
 
Old 01-26-2010, 04:14 PM   #3
GazL
Senior Member
 
Registered: May 2008
Posts: 3,231

Rep: Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828Reputation: 828
sed can do that for you quite easily.

e.g.
Code:
gazl@nix:/tmp$ cat testing
one
two
three
a
b
c
d
four
five
gazl@nix:/tmp$ sed -e '/three/,+4 d' < testing
one
two
four
five
gazl@nix:/tmp$
 
2 members found this post helpful.
Old 01-26-2010, 04:22 PM   #4
worm5252
Member
 
Registered: Oct 2004
Location: Atlanta
Distribution: CentOS, RHEL, HP-UX
Posts: 565

Rep: Reputation: 57
I found a 1 liner googling that will do exactly what you want.

Code:
awk '/search string here/{c=2}!(c&&c--)' file >newfile
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
 
1 members found this post helpful.
Old 01-27-2010, 09:13 AM   #5
RedHelix
LQ Newbie
 
Registered: Mar 2004
Location: BAWstun Massachusetts
Distribution: Red Hat
Posts: 9

Original Poster
Rep: Reputation: 0
Marvelous; thank you sir. Your google skills are strong
 
  


Reply

Tags
awk, grep, lines, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete Duplicate Lines in a file, leaving only the unique lines left xmrkite Linux - Software 6 01-14-2010 06:18 PM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM
Asterisk box to connect VoIP lines with normal telephone lines. sraju Linux - Software 1 11-08-2006 12:38 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
printer printing vertical lines at beginning and end of lines makhand Linux - Hardware 0 09-02-2005 02:03 PM


All times are GMT -5. The time now is 09:39 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration