How to grep a line with unknown number of its indented lines

grep

I often need to grep a line with its indented sections. For example, if the indented line number may be known, we would achieve this goal with the following:

$ lspci -vq | grep -i wireless -B 1 -A 5

    02:00.0 Network controller: Intel Corporation Wireless 7260 (rev 73)
        Subsystem: Intel Corporation Wireless-N 7260
        Flags: bus master, fast devsel, latency 0, IRQ 64
        Memory at c0600000 (64-bit, non-prefetchable) [size=8K]
        Capabilities: <access denied>

For example, the following command's output may or may not contain all of only one wireless lan information:

$ sudo iwlist wlan0 scan | grep -i "cell 13" -A 34
          Cell 13 - Address: 00:1A:2B:93:A7:9C
                    Channel:6
                    Frequency:2.437 GHz (Channel 6)
                    Quality=20/70  Signal level=-90 dBm  
                    Encryption key:on
                    ESSID:"NetMASTER Uydunet-E445"
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 18 Mb/s
                              24 Mb/s; 36 Mb/s; 54 Mb/s
                    Bit Rates:6 Mb/s; 9 Mb/s; 12 Mb/s; 48 Mb/s
                    Mode:Master
                    Extra:tsf=0000005b923c8227
                    Extra: Last beacon: 4648ms ago
                    IE: Unknown: 00164E65744D415354455220557964756E65742D45343435
                    IE: Unknown: 010882848B962430486C
                    IE: Unknown: 030106
                    IE: Unknown: 050400010000
                    IE: Unknown: 2A0100
                    IE: Unknown: 2F0100
                    IE: Unknown: 32040C121860
                    IE: Unknown: 2D1A2C181BFF00000000000000000000000000000000000000000000
                    IE: Unknown: 3D1606080400000000000000000000000000000000000000
                    IE: Unknown: DD090010180201F00C0000
                    IE: WPA Version 1
                        Group Cipher : CCMP
                        Pairwise Ciphers (1) : CCMP
                        Authentication Suites (1) : PSK
                    IE: Unknown: DD180050F2020101800003A4000027A4000042435E0062322F00
          Cell 14 - Address: 90:F6:52:90:C2:2F
                    Channel:6
                    Frequency:2.437 GHz (Channel 6)
                    Quality=21/70  Signal level=-89 dBm  
                    Encryption key:on
                    ESSID:"selma"
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
                              9 Mb/s; 12 Mb/s; 18 Mb/s

How can I make this output cropped to contain only one Cell information?

Edit

The working script is maintained at https://github.com/ceremcem/sgrep

Best Answer

If you want to output a REGEXP match and all the indented lines that are just after the match:

command | perl -ne '/^( *)/; $i = length $1; $j && $i >= $j and print, next; $j = 0; /REGEXP/ and $j = $i + 1, print'

replacing REGEXP by your regular expression.

For instance,

$ perl -ne '/^( *)/; $i = length $1; $j && $i >= $j and print, next; $j = 0; /a/ and $j = $i + 1, print' <<EOF
a
b
  c
  a
    b
    a
    c
      b
    d
  e
    b
  a
    c
e
EOF

outputs:

a
  a
    b
    a
    c
      b
    d
  a
    c
Related Question