-請問這樣的資料要如何用awk處理 - Linux

Table of Contents


我有格式如下的資料
Client dropped connection 52013
Compression error 2
Deactivating 1242
FTP error 0
FTPS not configured 174
Flow expired (sweeper) 2164495

我需要把資料重組成這樣的格式
sometext,type=Client_dropped_connection value=52013
sometext,type=Compression_error value=2
...
...
...

由於前面文字的部份每列欄位數非固定,透過awk拆解使用下列的語法

awk 'BEGIN{FS = "[ \t]+" ;ORS = "_"} {for(i=1;i<NF;i++) {print $i,"\n"}}'

原本預期能夠將前面的文字重組成類似這樣 -> Client_dropped_connection
結果輸出結果卻是像下面這樣

Client
_dropped
_connection
_Compression
_error
_Deactivating
_FTP
_error
_FTPS
_not
_configured
_Flow
_expired
_(sweeper)

不加 "\n" {print $i,"\n"} 輸出的結果卻是所有的字都在同一行
試過幾種方法都得不出我想要的結果
不知道是語法哪邊出錯?



--

All Comments

Andy avatarAndy2018-06-03
Catherine avatarCatherine2018-06-06
輸出的結果是每一行都是sometext,type= value=
Vanessa avatarVanessa2018-06-11
不過又給我一個方向了,我再試試,謝謝!
Michael avatarMichael2018-06-12
我用的是 gawk (GNU) 的版本
Donna avatarDonna2018-06-14
我在我的環境後來改成這樣才正常顯示
https://pastebin.com/RCvhCNdn
Noah avatarNoah2018-06-18
在我的環境中正規化比對有加$,會比對不到
Ula avatarUla2018-06-22
gsub在比對空格取代成底線的符號,用大大的語法match不到
Dorothy avatarDorothy2018-06-24
感謝goldie!!
Zanna avatarZanna2018-06-28
恭喜成功!題外話,PTT另有 RegExp 板