I am using
awk -F'[":]' '$2=="id"{printf("pri,%s,",$5)}$2=="name"{printf("%s,",$5)}$2=="objectId"{printf$4}$2=="polledName"{print$5}' | sed -e 's/, /,/g'
Which turns this
}, {
"id" : "1",
"name" : "host1",
"objectId" : 0001,
"polledName" : "192.168.1.1"
}, {
"id" : "2",
"name" : "host2",
"objectId" : 0002,
"polledName" : "192.168.1.2"
}, {
"id" : "3",
"name" : "host3",
"objectId" : 0003,
}, {
"id" : "4",
"name" : "host4",
"objectId" : 0004,
"polledName" : "192.168.1.3"
}, {
Into this
pri,1,host1,0001,192.168.1.1
pri,2,host2,0002,192.168.1.2
pri,3,host3,0003,pri,4,host4,0004,192.168.1.3
Any idea how this could be amended so that when the entry for polledName does not exist, it goes to the next line rather than wrapping to the current line, i.e. if $5 returns nothing then add a newline instead.
Here's the above code with the awk script pretty-printed by gawk -o-
so it's legible:
awk -F'[":]' '
$2 == "id" {
printf "pri,%s,", $5
}
$2 == "name" {
printf "%s,", $5
}
$2 == "objectId" {
printf $4
}
$2 == "polledName" {
print $5
}
' | sed -e 's/, /,/g'