如何使用grep来取得一行的唯一部分

时间:2019-02-02 18:34:58

标签: linux ubuntu unix grep

当我尝试发送ifconfig命令即时得到本

ath0  IEEE 802.11ng  ESSID:"test"  Nickname:""
      Mode:Master  Frequency:2.412 GHz  Access Point: 00:00:00:00:00:00
      Bit Rate:0 kb/s   Tx-Power=-4 dBm   Sensitivity=16/16
      Retry:off   RTS thr:off   Fragment thr:off
      Encryption key:off
      Power Management:off
      Link Quality=0/94  Signal level=-96 dBm  Noise level=-96 dBm
      Rx invalid nwid:475654423  Rx invalid crypt:0  Rx invalid frag:0
      Tx excessive retries:0  Invalid misc:0   Missed beacon:0

如何从例如线的这部分仅获得

ESSID:""test
Frequency:2.412 GHz

1 个答案:

答案 0 :(得分:0)

给出您的实际粘贴(而不考虑格式错误的示例输出):

$ cat ifconf 
ath0  IEEE 802.11ng  ESSID:"test"  Nickname:""
      Mode:Master  Frequency:2.412 GHz  Access Point: 00:00:00:00:00:00
      Bit Rate:0 kb/s   Tx-Power=-4 dBm   Sensitivity=16/16
      Retry:off   RTS thr:off   Fragment thr:off
      Encryption key:off
      Power Management:off
      Link Quality=0/94  Signal level=-96 dBm  Noise level=-96 dBm
      Rx invalid nwid:475654423  Rx invalid crypt:0  Rx invalid frag:0
      Tx excessive retries:0  Invalid misc:0   Missed beacon:0
$ grep -oE 'ESSID:"[^"]+"|Frequency:[^ ]+' ifconf
ESSID:"test"
Frequency:2.412

它实际上只是扩展的RegEx,而grep的-o标志仅输出完全匹配项,而不是匹配行。