Thursday, October 28, 2021

[SOLVED] grep surrounding lines in SunOS 5.9

Issue

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?


Solution

Try this:

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

Output:

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


Answered By - Cyrus