I'm trying to take a multiline file in the above format and make MySQL insert statement from it. End result aim is something similar to outputting: -
insert into ansible_db (ip, version) values ("$ip", "$version")
per line for each of the following data examples (this is the input file, I want the IP address and the result of the msg segment - ie 4.0.3): -
ok: [192.168.0.214] => {s "msg": "4.0.3"s}
ok: [192.168.0.210] => {s "msg": "4.0.8"s}
ok: [192.168.0.208] => {s "msg": "fdsajkghjfdka"s}
ok: [192.168.0.216] => {s "msg": "Potatoes""""s}
The following greps get the IP's and message out appropriately: -
$ip=grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
$version= grep -oP '(?<=\"\: \").*(?=\"s})' #format of the message will vary going forward.
I want to put it in a loop, but I'm struggling. Any advice appreciated.
awk -v FS='[][]|"' '{print "insert into ansible_db (ip, version) values ( "$2 " , "$6" )" }' yourInputFile