0

Trying to grep surrounding lines using SunOS 5.9, normally I would use grep with -B and -A to do so:

grep -B 3 -A 2 foo README.txt

However, in SunOS 5.9, grep don't support this feature with this error message:

grep: illegal option -- A

below is what I get from "man grep":http://www.freebsd.org/cgi/man.cgi?query=grep&apropos=0&sektion=0&manpath=SunOS+5.9&format=html

My example would be try to grep with keyword "Mirror" and those lines representing the state of its Submirror. Inputs to grep would be:

d6: Mirror
    Submirror 0: d61
      State: Okay
    Submirror 1: d62
      State: Okay
    Pass: 1
    Read option: ***
    Write option: ***
    Size: ***

d61: Submirror of d6
    State: Okay
    Size: ***
    Stripe 0:
    Device     Start Block  Dbase        State Reloc Hot Spare
    CCC          0     No            Okay   Yes


d62: Submirror of d6
    State: Okay
    Size: ***
    Stripe 0:
    Device     Start Block  Dbase        State Reloc Hot Spare
    BBB          0     No            Okay   Yes

In the above case, I want to get

d6: Mirror
    Submirror 0: d61
      State: Okay
    Submirror 1: d62
      State: Okay

How should I do so in SunOS 5.9?

Summer
  • 13
  • 4

2 Answers2

0

Try this:

sed -n '/Mirror/,/Pass:/{/Pass:/d;p;}' file

Output:

d6: Mirror
    Submirror 0: d61
      State: Okay
    Submirror 1: d62
      State: Okay
Cyrus
  • 84,225
  • 14
  • 89
  • 153
0

From your example, it looks like you only want to print lines after the match. If that's the case, then you can use this awk script:

awk '/Mirror/ { c = 5 } c && c--' file

It sets c to 5 when the pattern matches and prints lines as long as c is greater than 0 (so the next 4 lines).

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141