2

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

jigsaw
  • 165
  • 1
  • 16

1 Answers1

4

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

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

Output:

seattle | ip port1
spokane | ip port2
Cyrus
  • 84,225
  • 14
  • 89
  • 153