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!

Find expression & N number of lines below

Status
Not open for further replies.

stu78

Programmer
May 29, 2002
121
GB
Hi,

I need to find a line in a log file, then match the line 3 lines below;

For example, here is my input;

object: Test
id: 32190-12391-12984134-22
Name: Blah
Desc: This is the description
Type: 111-22912-12832
AttrId: 0


I want to match the line;
object: Test

Then match the line
Name: Blah

2 lines below.

How can I do this?
 
Brute force method as starting point:
awk '
/^object: Test/{getline;getline;print}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Oops, hit submit too fast:
awk '
/^object: Test/{getline;getline;print}
' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
sed -n '/^object: Test/{p;n;n;p;}' file.txt

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Code:
/^object: Test/ {line = NR; print}
NR - line == 2
 
ooops, that was blasphemous of me - sorry.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
mikevh, doesn't your awk program always print the 2nd line ?
 
Code:
/^object: Test/ { x=NR+2 }
NR==x

PHV said:
mikevh, doesn't your awk program always print the 2nd line?
It does. The only safe policy is: "Never post untested code." (Although I've probably done it myself.)
 
well.... just to be politically correct:

Code:
/^object: Test/ { x=FNR+2 }
FNR==x

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Vlad the Blasphemer: I presume you changed my code so as to handle more than one file. A test of your code:
----- file 1 -----
[tt]
object: Test
id: 32190-12391-12984134-22
Name: Blah
Desc: This is the description
Type: 111-22912-12832
AttrId: 0
[/tt]
----- file 2 -----
[tt]
object: Foo Bar
id: 32190-12391-12984134-22
Name: Blah
Desc: This is the description
Type: 111-22912-12832
AttrId: 0
[/tt]
Output:
[tt]
Name: Blah
Name: Blah
[/tt]

Code:
BEGIN { which=2 }
/^object: Test/ { n=NR+which; f=FNR+which }
NR==n && FNR==f
 
ah, I see - that's much better!

I should've read the classics:
"Never post untested code."

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I rarely post untested code, and always note it when I do.
In this case I tested against the sample input posted, though, and there the bug was not obvious since the pattern to match occurred before line 2. Thanks for pointing it out. I guess I should have said
Code:
/^object: Test/ {line = NR; print}
[b]line && NR - line == 2[/b]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top