YOGESH C
YOGESH C

Reputation: 1

Search pattern and print above lines till a pattern or empty line

I have a file containing

  zone name CTCFNFEVA-1DTX-CLUS3-SMA vsan 21
    pwwn 10:00:00:00:c9:82:71:c3
    pwwn 50:00:1f:e1:50:1e:d0:b9
    pwwn 50:00:1f:e1:50:1e:d0:bb
    pwwn 50:00:1f:e1:50:1e:d0:bd
    pwwn 50:00:1f:e1:50:1e:d0:bf

  zone name CTCFNFXCHC3P1-VTL-1DTX vsan 21
    pwwn 50:02:37:d3:44:57:00:01
    pwwn 50:02:37:d3:44:57:00:12
    pwwn 10:00:00:00:c9:97:08:9e


  zone name CHIIEHW02SS_HBAAE69_VMAX0424_FA2G0 vsan 21
  * fcid 0x160005 [pwwn 50:00:09:74:08:06:a1:84]
    pwwn 10:00:00:00:c9:62:ae:69

I have to search for 50:02:37:d3:44:57:00:12 and print the lines above and below it till an empty line.

For example, the output should be

  zone name CTCFNFXCHC3P1-VTL-1DTX vsan 21
    pwwn 50:02:37:d3:44:57:00:01
    pwwn 50:02:37:d3:44:57:00:12
    pwwn 10:00:00:00:c9:97:08:9e

How can I do it?

Upvotes: 0

Views: 86

Answers (2)

dawg
dawg

Reputation: 104032

Perl:

perl -nle 'BEGIN{$/="";} print if /50:02:37:d3:44:57:00:12/;'  file

Upvotes: 0

karakfa
karakfa

Reputation: 67507

awk to the rescue!

$ awk -v RS= '/50:02:37:d3:44:57:00:12/' file

Explanation: set awk to paragraph mode (-v RS=), print only the paragraph matching pattern.

Upvotes: 1

Related Questions