regular expression 小問題 - Linux

Edward Lewis avatar
By Edward Lewis
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

想要把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
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

Irma avatar
By Irma
at 2016-06-04T02:43
跳行不是要加反斜線?
Selena avatar
By Selena
at 2016-06-07T12:08
呃,錯了,那是要輸入。 覺得你要在 $ 後面加個\n
Dora avatar
By Dora
at 2016-06-12T02:14
抱歉。以上都不對~
Genevieve avatar
By Genevieve
at 2016-06-15T09:39
用 awk 來做比較快:
awk '{a=index($0, "P 0) byte=$0;else printf("byte=%d,
George avatar
By George
at 2016-06-17T18:14
data=%d", byte, $4);}' short_measurement_PING.txt
Lauren avatar
By Lauren
at 2016-06-22T16:07
貼爛了,重貼一次:
awk '{a=index($0, "PING");if (a == 0)
Una avatar
By Una
at 2016-06-22T23:05
byte=$0; else printf("byte=%d, data=%d", byte, $4);}'
short_measurement_PING.txt
Genevieve avatar
By Genevieve
at 2016-06-25T08:07
awk 'NF==1{print $1} $1 ~ /^PING$/ {print $4}'
Andy avatar
By Andy
at 2016-06-28T06:33
謝謝大家的建議,總算成功了
原來還有awk這麼好用的工具,,現在才知道 XD
Daph Bay avatar
By Daph Bay
at 2016-06-30T20:54
嗯,開始學 awk。謝謝~
Linda avatar
By Linda
at 2016-07-04T16:56
Jerr 的比較帥 // 感謝 Jerr
Dorothy avatar
By Dorothy
at 2016-07-08T23:08
為什麼不直接sed 's///g' XXXX.txt 這樣做就好,比較簡
Hedda avatar
By Hedda
at 2016-07-11T16:35
單,cat xxxx.txt | sed 'g///s' 有點多此一舉
寫錯了 'g///s'

Ubuntu 更新完無法開啟 gnome-terminal

Lydia avatar
By Lydia
at 2016-05-31T21:53
請問有人最近一次更新完後無法開啟終端機的嗎? 我筆電和桌電都發生了這種狀況,但換個使用者就 可以開啟了,請問有什麼解決方法嗎? 錯誤訊息: Error creating terminal: Message recipient disconnected from message bus without rep ...

vim 好用 plugin 分享

Edward Lewis avatar
By Edward Lewis
at 2016-05-30T12:00
各位大大好 小弟與朋友們最近開始搜集與整理好用的 vim plugin。 想說 Linux 版上應該會有許多大大用 vim, 因此,貼在此版分享,希望沒有不禮貌。:) 在此分享給大家,我們搜集的 plugins 及 vimscript。 https://github.com/18z/VIM-tips 各 ...

ntp pool

Leila avatar
By Leila
at 2016-05-29T17:36
一個月前在BS2發了篇文測試轉信功能,看來本板是不打算修轉信了... https://groups.google.com/forum/#!topic/tw.bbs.comp.linux/e-AuoxNAdz4 (事實上PTT就連轉信標頭亂碼的狀況也不處理,或許不轉信也是少點汙染) 回題.總之我要說的是,台灣 ...

Ubuntu Server 安裝好多問題 (15.10)

Delia avatar
By Delia
at 2016-05-29T17:15
跟這台Server纏鬥了快兩天了,重裝N次…來問問題了囧 硬體配置: http://goo.gl/4YZYLf RAM:配16GB 主機板:Intel DBS1200V3RPS (軟體RAID) 硬碟配置:4顆WD2004FBYZ SATA3 企業級硬碟,配置RAID5 本來要直接裝16.04(以下 ...

windows 真得很容易當機嗎?

Kumar avatar
By Kumar
at 2016-05-29T13:07
雖然和 linux 沒什麼關係……,但還是有點好奇。 因為我覺得問在 windows 板得不到我想要的答案。 或者說, linux 真得很不容易當機嗎? 許多 linux 推坑文開頭就是舉了幾個和 windows 的對比, 其中多包含一個 and#34;windows 容易當機and#34; 的事實。 我 ...