Use two field spearators in awk [duplicate]

2019-02-20 03:07发布

This question already has an answer here:

I am trying to parse a string like this with bash

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 .

I want output like this

seattle | ip port1
spokane | ip port2

I was trying to use awk with this

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

but its not printing the details that I want

标签: bash parsing awk
1条回答
叼着烟拽天下
2楼-- · 2019-02-20 03:36

Use -F and [] to set multiple field separators.

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

Output:

seattle | ip port1
spokane | ip port2
查看更多
登录 后发表回答