[問題] regular expression 小問題

看板Linux作者 (HAHAHA~)時間9年前 (2016/06/01 05:55), 9年前編輯推噓5(5013)
留言18則, 5人參與, 最新討論串1/2 (看更多)
大家好, 有一份文件(https://goo.gl/2Vc3Ui): 146460673 PING 10.0.0.6 (10.0.0.6): 56 data bytes 想要把14646067356抓出來, 自己試過下列的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

06/01 08:49, , 1F
跳行不是要加反斜線?
06/01 08:49, 1F

06/01 08:53, , 2F
呃,錯了,那是要輸入。 覺得你要在 $ 後面加個\n
06/01 08:53, 2F

06/01 09:46, , 3F
抱歉。以上都不對~
06/01 09:46, 3F

06/01 09:57, , 4F
用 awk 來做比較快:
06/01 09:57, 4F

06/01 09:57, , 5F
awk '{a=index($0, "P 0) byte=$0;else printf("byte=%d,
06/01 09:57, 5F

06/01 09:58, , 6F
data=%d", byte, $4);}' short_measurement_PING.txt
06/01 09:58, 6F

06/01 09:59, , 7F
貼爛了,重貼一次:
06/01 09:59, 7F

06/01 09:59, , 8F
awk '{a=index($0, "PING");if (a == 0)
06/01 09:59, 8F

06/01 10:00, , 9F
byte=$0; else printf("byte=%d, data=%d", byte, $4);}'
06/01 10:00, 9F

06/01 10:00, , 10F
short_measurement_PING.txt
06/01 10:00, 10F

06/01 15:08, , 11F
awk 'NF==1{print $1} $1 ~ /^PING$/ {print $4}'
06/01 15:08, 11F

06/01 16:33, , 12F
謝謝大家的建議,總算成功了
06/01 16:33, 12F

06/01 16:33, , 13F
原來還有awk這麼好用的工具,,現在才知道 XD
06/01 16:33, 13F

06/02 06:43, , 14F
嗯,開始學 awk。謝謝~
06/02 06:43, 14F

06/02 13:39, , 15F
Jerr 的比較帥 // 感謝 Jerr
06/02 13:39, 15F

06/03 15:49, , 16F
為什麼不直接sed 's///g' XXXX.txt 這樣做就好,比較簡
06/03 15:49, 16F

06/03 15:50, , 17F
單,cat xxxx.txt | sed 'g///s' 有點多此一舉
06/03 15:50, 17F

06/03 15:50, , 18F
寫錯了 'g///s'
06/03 15:50, 18F
文章代碼(AID): #1NJWXM7O (Linux)
文章代碼(AID): #1NJWXM7O (Linux)