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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
01-26-2010, 03:48 PM
|
#1
|
LQ Newbie
Registered: Mar 2004
Location: BAWstun Massachusetts
Distribution: Red Hat
Posts: 9
Rep:
|
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.
|
01-26-2010, 03:52 PM
|
#2
|
Member
Registered: Oct 2004
Location: Atlanta
Distribution: CentOS, RHEL, HP-UX, OS X
Posts: 567
Rep:
|
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.
|
|
|
01-26-2010, 04:14 PM
|
#3
|
LQ Veteran
Registered: May 2008
Posts: 7,006
|
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.
|
01-26-2010, 04:22 PM
|
#4
|
Member
Registered: Oct 2004
Location: Atlanta
Distribution: CentOS, RHEL, HP-UX, OS X
Posts: 567
Rep:
|
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.
|
01-27-2010, 09:13 AM
|
#5
|
LQ Newbie
Registered: Mar 2004
Location: BAWstun Massachusetts
Distribution: Red Hat
Posts: 9
Original Poster
Rep:
|
Marvelous; thank you sir. Your google skills are strong
|
|
|
All times are GMT -5. The time now is 06:35 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|