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!

extracting lines before and after pattern. 1

Status
Not open for further replies.

Czar24

Technical User
Mar 22, 2004
16
US
Here's what I'm trying to do.

I'm printing a pattern out of a source file by matching it using:

nawk'
.
.
.
/start_string/,/stop_string/{print > output.file}
.
.
.
' input.file


Sample input.file

.
.
.
1111blah
1112blah
1113blah
1114header information 1
1115header information 2
1116blah blah start_string blah
1117recording
1118all
1119of
1120this
1121blah blah stop_string blah
1122footer information 1
1123footer information 2
1124blah
1125blah
1126blah
1127blah
.
.
.

Right now I'm getting an output.file that looks like:
1116blah blah start_string blah
1117recording
1118all
1119of
1120this
1121blah blah stop_string blah

I would like an output.file that looks like:

1114header information 1
1115header information 2
1116blah blah start_string blah
1117recording
1118all
1119of
1120this
1121blah blah stop_string blah
1122footer information 1
1123footer information 2

None of the header or footer information is unique enough to pattern directly and I was wondering if there is a way to call the pattern match and have it start/stop "n" lines (this case n=2) before/after the pattern is encountered.
Any hints to get me back on track?

Thanks in advance!

Scott
 
This may get you started

{a1=a2;a2=a0;a0=$0}
/start_string/{print a1 > output.file;print a2 > output.file}
/start_string/,/stop_string/{print > output.file}
/stop_string/{getline;print > output.file;getline; print > output.file}



CaKiwi
 
I was working something along those lines, but was afraid it would have caused errors, but alas this works like a champ! I'm getting some other unexpected outputs now, but it's something I can quash pretty quick!


Thanks again CaKiwi!



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top