sed或awk从输出文件列字符串模式中提取值

时间:2016-09-30 04:45:03

标签: awk sed

我有一个如下文件:

  Cell 01 - Address: 58:6D:8F:D1:A9:07                    Quality=52/70  Signal level=-58 dBm                      Encryption key:on                    ESSID:"money-plant"                    Extra: Last beacon: 80ms ago
  Cell 02 - Address: 10:C3:7B:55:73:38                    Quality=45/70  Signal level=-65 dBm                      Encryption key:on                    ESSID:"JDBX"                    Extra: Last beacon: 80ms ago
  Cell 03 - Address: 64:09:80:5F:7D:AF                    Quality=61/70  Signal level=-49 dBm                      Encryption key:on                    ESSID:"NExT"                    Extra: Last beacon: 80ms ago
  Cell 04 - Address: 64:66:B3:9D:82:76                    Quality=49/70  Signal level=-61 dBm                      Encryption key:on                    ESSID:"home-router"                    Extra: Last beacon: 80ms ago
  Cell 05 - Address: 08:86:3B:30:88:6C                    Quality=42/70  Signal level=-68 dBm                      Encryption key:on                    ESSID:"SoundEyeWifi"                    Extra: Last beacon: 80ms ago
  Cell 06 - Address: 00:0B:81:9A:5F:37                    Quality=33/70  Signal level=-77 dBm                      Encryption key:on                    ESSID:"IROAD_AEV_9A5F37"                    Extra: Last beacon: 80ms ago
  Cell 07 - Address: 74:A2:E6:CF:94:D0                    Quality=48/70  Signal level=-62 dBm                      Encryption key:on                    ESSID:"NUS"                    Extra: Last beacon: 80ms ago
  Cell 08 - Address: AC:F1:DF:26:FD:E4                    Quality=39/70  Signal level=-71 dBm                      Encryption key:on                    ESSID:"iNote"                    Extra: Last beacon: 80ms ago
  Cell 09 - Address: 74:A2:E6:CF:94:D1                    Quality=52/70  Signal level=-58 dBm                      Encryption key:on                    ESSID:"eduroam"                    Extra: Last beacon: 80ms ago
  Cell 10 - Address: 74:A2:E6:CF:94:D3                    Quality=51/70  Signal level=-59 dBm                      Encryption key:on                    ESSID:"\x00"                    Extra: Last beacon: 80ms ago
  Cell 11 - Address: 74:A2:E6:CF:94:D5                    Quality=49/70  Signal level=-61 dBm                      Encryption key:on                    ESSID:"\x00"                    Extra: Last beacon: 80ms ago
  Cell 12 - Address: 74:A2:E6:CF:94:D8                    Quality=50/70  Signal level=-60 dBm                      Encryption key:on                    ESSID:"\x00"                    Extra: Last beacon: 80ms ago

使用sedawk或其他什么,我想要以下输出:

58:6D:8F:D1:A9:07 -58 money-plant 80ms
10:C3:7B:55:73:38 -65 JDBX 80ms
64:09:80:5F:7D:AF -78 Next 80ms
64:66:B3:9D:82:76 -71 home-router 80ms
...

如何实现这一目标?

5 个答案:

答案 0 :(得分:3)

$ awk -F'[ "=]+' '{print $6, $11, $16, $20}' file
58:6D:8F:D1:A9:07 -58 money-plant 80ms
10:C3:7B:55:73:38 -65 JDBX 80ms
64:09:80:5F:7D:AF -49 NExT 80ms
64:66:B3:9D:82:76 -61 home-router 80ms
08:86:3B:30:88:6C -68 SoundEyeWifi 80ms
00:0B:81:9A:5F:37 -77 IROAD_AEV_9A5F37 80ms
74:A2:E6:CF:94:D0 -62 NUS 80ms
AC:F1:DF:26:FD:E4 -71 iNote 80ms
74:A2:E6:CF:94:D1 -58 eduroam 80ms
74:A2:E6:CF:94:D3 -59 \x00 80ms
74:A2:E6:CF:94:D5 -61 \x00 80ms
74:A2:E6:CF:94:D8 -60 \x00 80ms

答案 1 :(得分:1)

cat input_list_file | awk '{print $5,$8,$12,$16}' | sed 's/level=//g' | sed 's/ESSID://g' | sed 's/"//g'

答案 2 :(得分:1)

使用greppcre

$ grep -oP 'Address: \K\S+|Signal level=\K\S+|ESSID:"\K[^"]+|\d+ms' ip.txt | pr -4 -ats' '
58:6D:8F:D1:A9:07 -58 money-plant 80ms
10:C3:7B:55:73:38 -65 JDBX 80ms
64:09:80:5F:7D:AF -49 NExT 80ms
64:66:B3:9D:82:76 -61 home-router 80ms
08:86:3B:30:88:6C -68 SoundEyeWifi 80ms
00:0B:81:9A:5F:37 -77 IROAD_AEV_9A5F37 80ms
74:A2:E6:CF:94:D0 -62 NUS 80ms
AC:F1:DF:26:FD:E4 -71 iNote 80ms
74:A2:E6:CF:94:D1 -58 eduroam 80ms
74:A2:E6:CF:94:D3 -59 \x00 80ms
74:A2:E6:CF:94:D5 -61 \x00 80ms
74:A2:E6:CF:94:D8 -60 \x00 80ms
  • 使用正面lookbehind修剪匹配的字符串

要整齐地设置样式,请省略分隔符(此处不显示,但列排列)

$ grep -oP 'Address: \K\S+|Signal level=\K\S+|ESSID:"\K[^"]+|\d+ms' ip.txt | pr -4 -at
58:6D:8F:D1:A9:07 -58           money-plant       80ms
10:C3:7B:55:73:38 -65           JDBX          80ms
64:09:80:5F:7D:AF -49           NExT          80ms
...

答案 3 :(得分:1)

使用来自String-Manipulation Functionssubstr的GNU awk及其自己的幻想printf,可以将其作为

awk '{split($8, a, "level=");b=substr($12,7);gsub(/"/,"",b);printf "%-16s %s %-16s %-16s\n", $5, a[2], b, $16}' file

按预期生成精美的输出格式。

58:6D:8F:D1:A9:07 -58 money-plant      80ms
10:C3:7B:55:73:38 -65 JDBX             80ms
64:09:80:5F:7D:AF -49 NExT             80ms
64:66:B3:9D:82:76 -61 home-router      80ms
08:86:3B:30:88:6C -68 SoundEyeWifi     80ms
00:0B:81:9A:5F:37 -77 IROAD_AEV_9A5F37 80ms
74:A2:E6:CF:94:D0 -62 NUS              80ms
AC:F1:DF:26:FD:E4 -71 iNote            80ms
74:A2:E6:CF:94:D1 -58 eduroam          80ms
74:A2:E6:CF:94:D3 -59 \x00             80ms
74:A2:E6:CF:94:D5 -61 \x00             80ms
74:A2:E6:CF:94:D8 -60 \x00             80ms

答案 4 :(得分:0)

使用 Sed ,因为每个人都想使用awk。我正在使用分组:

sed 's/\(^.*Address: \)\([A-Z0-9:]*\)\( .*level=\)\(\-[0-9]*\)\( .*ESSID:\"\)\(.*\)\(\".*beacon: \)\(.*\)\( ago.*\)/\2 \4 \6 \8/g' tmp

输出:

58:6D:8F:D1:A9:07 -58 money-plant 80ms
10:C3:7B:55:73:38 -65 JDBX 80ms
64:09:80:5F:7D:AF -49 NExT 80ms
64:66:B3:9D:82:76 -61 home-router 80ms
08:86:3B:30:88:6C -68 SoundEyeWifi 80ms
00:0B:81:9A:5F:37 -77 IROAD_AEV_9A5F37 80ms
74:A2:E6:CF:94:D0 -62 NUS 80ms
AC:F1:DF:26:FD:E4 -71 iNote 80ms
74:A2:E6:CF:94:D1 -58 eduroam 80ms
74:A2:E6:CF:94:D3 -59 \x00 80ms
74:A2:E6:CF:94:D5 -61 \x00 80ms
74:A2:E6:CF:94:D8 -60 \x00 80ms