简体   繁体   中英

Using grep in Linux to get two strings and sort only based on one

My file looks like this:

BSS 00:1a:1e:5b:8b:21 (on wlan0) -- associated
    TSF: 39741405897 usec (0d, 11:02:21)
    freq: 2437
    beacon interval: 100
    capability: ESS ShortPreamble ShortSlotTime (0x0421)
    signal: -71.00 dBm
    last seen: 104 ms ago
    Information elements from Probe Response frame:
    ...
    ...
    ... 
BSS 00:1c:1c:5a:8e:23 (on wlan0)
    TSF: 397414345497 usec (0d, 11:02:21)
    freq: 2433
    beacon interval: 100
    capability: ESS ShortPreamble ShortSlotTime (0x0421)
    signal: -76.00 dBm
    last seen: 104 ms ago
    Information elements from Probe Response frame:
    ...
    ...
    ...

And so on. (It's basically the output of iw dev wlan0 scan ).

Now I want to get only BSS 00:1a:1e:5b:8b:21 and signal: -72.00 dBm (on the same line), and the lines should be sorted based on the signal part. Could anyone please help me with the grep command I should use?

Thank you.

Try using awk followed by sort

awk '/^[ ]*BSS/ {p=1; a=$1" "$2} p && /^[ ]*signal/ {p=0; a=a" "$0; print a}' file | sort -n -k4

Output for your sample input file

BSS 00:1c:1c:5a:8e:23     signal: -76.00 dBm
BSS 00:1a:1e:5b:8b:21     signal: -71.00 dBm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM