Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

need to delete text lines
I want to find a pattern in a text file and delete the 2 lines in
front of the found pattern to 1 line after the pattern throughout the
file example:

line
1  this is a test
2  to try to simulate
3  what I want to
4  do is this for real
5  text to look for
6  parrot
7  corn
8  apple
9  poll
10 this is the path

I want to find the pattern "text to look for" and remove line 3 - 6
leaving
line
1  this is a test
2  to try to simulate
3  corn
4  apple
5  poll
6  this is the path

Report this thread to moderator Post Follow-up to this message
Old Post
usatim
09-02-04 01:55 PM


Re: need to delete text lines
Wow! How rare it is for someone to post the an example of their input
and output files! I wish everyone did this.

You will need to save/buffer your input and then print those lines a
little later, after you've determined that "text to look for" hasn't
appeared and therefore it is 'safe' to output those buffered lines.

But at the end of the program, you will have to print those lines
still in the buffer, you can't just exit your program.

I won't attempt to write the optimum or most efficient program, but
probably someone else will contribute one.

I did give this a quick test. It might not work correctly if "text to
look for" was the last line in the file or if you had two "text to
look for" lines consecutively.

{
# Save every line initially
line[NR] = $0
}

/text to look for/ {
# Delete two buffered lines and the current line
delete line[NR-2]
delete line[NR-1]
delete line[NR]

# Skip the next line
# It doesn't have to go into the buffer
getline
}

!/text to look for/ {
# Now it is safe to print one buffered line (if it exists)
if ((NR-2) in line) print line[NR-2]
delete line[NR-2]
}

END {
# Hmm. I don't know what the value of NR is here.
# I'll assume it's unchanged
# There are only two lines in the buffer
for (n = NR-1; n <= NR; n++) print line[n]
}

DKM


On 26 Aug 2004 07:56:39 -0700, tillman.h.craft@usa-spaceops.com
(usatim) wrote:

>I want to find a pattern in a text file and delete the 2 lines in
>front of the found pattern to 1 line after the pattern throughout the
>file example:
>
>line
>1  this is a test
>2  to try to simulate
>3  what I want to
>4  do is this for real
>5  text to look for
>6  parrot
>7  corn
>8  apple
>9  poll
>10 this is the path
>
>I want to find the pattern "text to look for" and remove line 3 - 6
>leaving
>line
>1  this is a test
>2  to try to simulate
>3  corn
>4  apple
>5  poll
>6  this is the path


To contact me directly, send EMAIL to (single letters all)
DEE_KAY_EMM AT EarthLink.net. [For example X_X_X@EarthLink.net.]

Report this thread to moderator Post Follow-up to this message
Old Post
Doug McClure
09-02-04 08:56 PM


Re: need to delete text lines

usatim wrote:
> I want to find a pattern in a text file and delete the 2 lines in
> front of the found pattern to 1 line after the pattern throughout the
> file example:
>
> line
> 1  this is a test
> 2  to try to simulate
> 3  what I want to
> 4  do is this for real
> 5  text to look for
> 6  parrot
> 7  corn
> 8  apple
> 9  poll
> 10 this is the path
>
> I want to find the pattern "text to look for" and remove line 3 - 6
> leaving
> line
> 1  this is a test
> 2  to try to simulate
> 3  corn
> 4  apple
> 5  poll
> 6  this is the path

This should get you close (untested):

gawk '/text to look for/{ c=c-2; s=1; next }
s { s--; next }
{ a[++c]=$0 }
END {for(i=1;i<=c;i++) print a[i]}'

Regards,

Ed.


Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
09-02-04 08:56 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:03 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.