Skip to content

Howto grab additional lines when grep is ancient

In modern grep utilities can use -A (after) and -B (before) to return additional lines from your search. Solaris 11 still does not have a grep that can do this. Several options exist but this is one that worked for me.

Example output without grep. I wanted Vendor and one extra line after for Size.

# iostat -E
[...]
ssd24     Soft Errors: 0 Hard Errors: 0 Transport Errors: 0
Vendor: 3PARdata Product: VV               Revision: 3123 Serial No: 1203842
Size: 268.44GB <268435456000 bytes>
Media Error: 0 Device Not Ready: 0 No Device: 0 Recoverable: 0
Illegal Request: 0 Predictive Failure Analysis: 0
ssd25     Soft Errors: 0 Hard Errors: 0 Transport Errors: 0
Vendor: 3PARdata Product: VV               Revision: 3123 Serial No: 1203842
Size: 273.80GB <273804165120 bytes>
Media Error: 0 Device Not Ready: 0 No Device: 0 Recoverable: 0
[..]

One possible way with awk. "ssd" was my start and "2" was my number of lines after.

# iostat -E | awk '/'"ssd"'/{l=1;count=NR;next} l>0 && NR-count < '"2"+1' {print}'
Vendor: 3PARdata Product: VV Revision: 3123 Serial No: .....
Size: 268.44GB
Vendor: 3PARdata Product: VV Revision: 3123 Serial No: .....
Size: 273.80GB
Vendor: 3PARdata Product: VV Revision: 3123 Serial No: .....
Size: 536.87GB
Vendor: SUN Product: ZFS Storage 7330 Revision: 1.0 Serial No:
Size: 37.58GB
Published inSolaris