2017-06-17 64 views
1

我試圖解析字符串像這樣使用bash使用兩個現場spearators

OPS |all|1234|ip:port1|name|state|number|id|phone=123;zip=123;state=AB;city=seattle . 
OPS |all|1234|ip:port2|name|state|number|id|phone=123;zip=123;state=AB;city=spokane . 

我想這樣

seattle | ip port1 
spokane | ip port2 

我試圖用awk這個

輸出
awk -F'|' '{ n = split($4,array,"|"); printf "%s, %s\n", $4, array[n] }' file.txt 

但它沒有打印我想要的細節

回答

4

使用-F[]來設置多個字段分隔符。

awk -F '[|:= ]' '{print $14,"|",$4,$5}' file 

輸出:

 
seattle | ip port1 
spokane | ip port2 
+0

我改變了輸入一點(OPS後有一個空格)的輸出是走出這樣 'AB;城市| 1234 ip AB; city | 1234 ip' – jigsaw

+0

如果插入一個附加的字段分隔符,則輸出也會更改。 – Cyrus

+0

是的,我只是改變了你的答案增加1,它的工作 – jigsaw