Friday, October 29, 2010

greping multiple lines

I just found a neat way to grep multiple lines when you only know the contents of one of those lines --

Use -B n to see n lines before the match and -A m to see m lines after the match.

ie. grep -B2 -A2

Example --


root@localhost:~# echo ' Cell 52 - Address: 00:24:6C:AB:26:D3
Protocol:802.11b/g/n
ESSID:"StevesAP"
Mode:Managed
Frequency:2.462 GHz (Channel 11)
Quality:100/100 Signal level:-25 dBm Noise level:-92 dBm
Encryption key:on
Bit Rates:54 Mb/s
IE: IEEE 802.11i/WPA2 Version 1
' | grep -B2 -A2 -i steve

Cell 52 - Address: 00:24:6C:AB:26:D3
Protocol:802.11b/g/n
ESSID:"StevesAP"
Mode:Managed
Frequency:2.462 GHz (Channel 11)