regular expression 小問題 - Linux
By Edward Lewis
at 2016-06-01T05:55
at 2016-06-01T05:55
Table of Contents
大家好,
有一份文件(https://goo.gl/2Vc3Ui):
146460673
PING 10.0.0.6 (10.0.0.6): 56 data bytes
想要把146460673及56抓出來,
自己試過下列的regular expression:
cat short_measurement_PING.txt | sed -r 's/([0-9]+).*PING.*([0-9]+) data bytes/\1 \2/g'
cat short_measurement_PING.txt | sed -r 's/([0-9]+)$.*PING.*([0-9]+) data bytes/\1 \2/g'
cat short_measurement_PING.txt | sed -r 's/^([0-9]+)$^.*PING.*([0-9]+) data bytes/\1 \2/g'
不過完全沒作用,
想請問大家有什麼建議嗎?
謝謝~
※ 編輯: decken (212.201.78.21), 06/01/2016 06:10:26
有一份文件(https://goo.gl/2Vc3Ui):
146460673
PING 10.0.0.6 (10.0.0.6): 56 data bytes
想要把146460673及56抓出來,
自己試過下列的regular expression:
cat short_measurement_PING.txt | sed -r 's/([0-9]+).*PING.*([0-9]+) data bytes/\1 \2/g'
cat short_measurement_PING.txt | sed -r 's/([0-9]+)$.*PING.*([0-9]+) data bytes/\1 \2/g'
cat short_measurement_PING.txt | sed -r 's/^([0-9]+)$^.*PING.*([0-9]+) data bytes/\1 \2/g'
不過完全沒作用,
想請問大家有什麼建議嗎?
謝謝~
※ 編輯: decken (212.201.78.21), 06/01/2016 06:10:26
→ Ice9: 跳行不是要加反斜線? 06/01 08:49
→ Ice9: 呃,錯了,那是要輸入。 覺得你要在 $ 後面加個\n 06/01 08:53
→ Ice9: 抱歉。以上都不對~ 06/01 09:46
推 antontw: 用 awk 來做比較快: 06/01 09:57
→ antontw: awk '{a=index($0, "P 0) byte=$0;else printf("byte=%d, 06/01 09:57
→ antontw: data=%d", byte, $4);}' short_measurement_PING.txt 06/01 09:58
→ antontw: 貼爛了,重貼一次: 06/01 09:59
→ antontw: awk '{a=index($0, "PING");if (a == 0) 06/01 09:59
→ antontw: byte=$0; else printf("byte=%d, data=%d", byte, $4);}' 06/01 10:00
→ antontw: short_measurement_PING.txt 06/01 10:00
推 Jerr: awk 'NF==1{print $1} $1 ~ /^PING$/ {print $4}' 06/01 15:08
→ decken: 謝謝大家的建議,總算成功了 06/01 16:33
→ decken: 原來還有awk這麼好用的工具,,現在才知道 XD 06/01 16:33
推 Ice9: 嗯,開始學 awk。謝謝~ 06/02 06:43
推 antontw: Jerr 的比較帥 // 感謝 Jerr 06/02 13:39
推 pizzahut: 為什麼不直接sed 's///g' XXXX.txt 這樣做就好,比較簡 06/03 15:49
→ pizzahut: 單,cat xxxx.txt | sed 'g///s' 有點多此一舉 06/03 15:50
→ pizzahut: 寫錯了 'g///s' 06/03 15:50
Tags:
Linux
All Comments
By Irma
at 2016-06-04T02:43
at 2016-06-04T02:43
By Selena
at 2016-06-07T12:08
at 2016-06-07T12:08
By Dora
at 2016-06-12T02:14
at 2016-06-12T02:14
By Genevieve
at 2016-06-15T09:39
at 2016-06-15T09:39
By George
at 2016-06-17T18:14
at 2016-06-17T18:14
By Lauren
at 2016-06-22T16:07
at 2016-06-22T16:07
By Una
at 2016-06-22T23:05
at 2016-06-22T23:05
By Genevieve
at 2016-06-25T08:07
at 2016-06-25T08:07
By Andy
at 2016-06-28T06:33
at 2016-06-28T06:33
By Daph Bay
at 2016-06-30T20:54
at 2016-06-30T20:54
By Linda
at 2016-07-04T16:56
at 2016-07-04T16:56
By Dorothy
at 2016-07-08T23:08
at 2016-07-08T23:08
By Hedda
at 2016-07-11T16:35
at 2016-07-11T16:35
Related Posts
Ubuntu 更新完無法開啟 gnome-terminal
By Lydia
at 2016-05-31T21:53
at 2016-05-31T21:53
vim 好用 plugin 分享
By Edward Lewis
at 2016-05-30T12:00
at 2016-05-30T12:00
ntp pool
By Leila
at 2016-05-29T17:36
at 2016-05-29T17:36
Ubuntu Server 安裝好多問題 (15.10)
By Delia
at 2016-05-29T17:15
at 2016-05-29T17:15
windows 真得很容易當機嗎?
By Kumar
at 2016-05-29T13:07
at 2016-05-29T13:07