1828853 Members
2424 Online
109985 Solutions
New Discussion

Re: help lines script

 
Jairo Campana
Trusted Contributor

help lines script

Hi, I have this lines , my patern to find is example: 50:06:04:8c:52:a5:62:88 , I need extract the two previos lines
include Name hosts

zone: HOSTB-SRV:8b_1
20:00:00:e0:69:41:4c:3f
50:06:04:84:49:af:03:a7
zone: HOSTA-SRV:9c0
20:00:00:e0:69:41:4c:3f
50:06:04:8c:52:a5:62:88

awk '/50:06:04:8c:52:a5:62:88/' output.tmp
50:06:04:8c:52:a5:62:88

grep "50:06:04:8c:52:a5:62:88" output.tmp
50:06:04:8c:52:a5:62:88
legionx
2 REPLIES 2
Hein van den Heuvel
Honored Contributor

Re: help lines script

Just have awk stash away the lines as it read them , for example in l2 and l1 and print those on match.

With hard-coded match:

$ awk '/50:06:04:8c:52:a5:62:88/{print l2; print l1} {l2=l1;l1=$0}' x
zone: HOSTA-SRV:9c0
20:00:00:e0:69:41:4c:3f

Using a variable to look for...

$ id="50:06:04:8c:52:a5:62:88"
$ awk -vid=$id '$1==id {print l2; print l1} {l2=l1;l1=$0}' x
zone: HOSTA-SRV:9c0
20:00:00:e0:69:41:4c:3f

hth,
Hein.

James R. Ferguson
Acclaimed Contributor

Re: help lines script

Hi:

One way (in Perl):

perl -e '@lines=reverse (<>);for (@lines) {push @rslt,$_ if /^50:06:04:8c/../^zone/};print reverse @rslt' file

zone: HOSTA-SRV:9c0
20:00:00:e0:69:41:4c:3f
50:06:04:8c:52:a5:62:88

Regards!

...JRF...