Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search and replace 1

Status
Not open for further replies.

blainepruitt

Technical User
Apr 18, 2002
105
US
Greetings all,

I have a large log file that I would like to clean up. In order this I am using vi and doing a search for a string, but I would like to delete the next 52 lines after it finds that string. I could have sworn I found documentation about this in the past. Does anyone know if this can be done, and if so how I would go about handling it? I've done some searches on the internet but I can't seem to find anything that fits my query.

Thanks in advance for any help you can provide.

-bp
 
Code:
# Prints the line where string is found,
# but skips the following lines.
BEGIN { lines_to_delete = 52 }

deleting { deleting--; next }

/foo bar/ { deleting = lines_to_delete }

{ print }
Save as del52.awk and run with
[tt]awk -f del52.awk infile >outfile[/tt]

Let me know whether or not this helps.

If you have nawk, use it instead of awk because on some systems awk is very old and lacks many useful features. For an introduction to Awk, see faq271-5564.
 
THANKS!!! That worked great! Just out of curiosity, do you know of any good books or online references that would have valuable information about sed/awk/nawk?

Again, thanks for your help, you just saved me alot of time.

regards,
-bp
 
The FAQ that I wrote will get you started with awk: FAQ271-5564.

Gawk and Mawk come with good documentation. On Unix, try "man awk".
 
Hi futurelet,

Could you please explain how this routine works?

Thanks
 
[tt]
deleting--
[/tt]
is the same as [tt]deleting -= 1[/tt] which is the same as [tt]deleting = deleting - 1[/tt].

The rest is explained in FAQ271-5564.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top