0

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.

5
  • At least to me, it's not clear what part of this is the input format, and what part is the output format. I think it could use some editing to clarify what each looks like/needs to look like. Commented Sep 25, 2017 at 11:49
  • 1
    no need to bash loop, try awk -v FS='[][]|"' '{print "insert into ansible_db (ip, version) values ( "$2 " , "$6" )" }' yourInputFile Commented Sep 25, 2017 at 11:52
  • PS that literally did it, thank you so much. Commented Sep 25, 2017 at 12:01
  • End result will be code vulnerable to a SQL injection attack. Don't do this. Commented Sep 25, 2017 at 14:23
  • @LetsScriptThis thanks for the edit... Commented Sep 26, 2017 at 9:04

2 Answers 2

1

In this case, while-loop is not required. If your file is having the consistent structure like you provided in the question following command should print the required lines.

awk -v FS='[][]|"' '{print "insert into ansible_db (ip, version) values ( \""$2 "\" , \""$6"\" )" }' yourInputFile

Note that above command will display the result in std out, if you want to execute then you can use |bash in the end of the command like:

awk -v FS='[][]|"' '{print "insert into ansible_db (ip, version) values ( "$2 " , "$6" )" }' yourInputFile |bash
Sign up to request clarification or add additional context in comments.

Comments

0

Note, MySql VALUES syntax requires complex string arguments to be enclosed with quotes.
To obtain valid MySql insert statements use the following approach:

awk '{ gsub(/[\[\]]/,"",$2); sub("\"+s}","\"",$6); 
       print "insert into ansible_db (ip, version) values (\""$2"\", "$6");" }' file

The output:

insert into ansible_db (ip, version) values ("192.168.0.214", "4.0.3");
insert into ansible_db (ip, version) values ("192.168.0.210", "4.0.8");
insert into ansible_db (ip, version) values ("192.168.0.208", "fdsajkghjfdka");
insert into ansible_db (ip, version) values ("192.168.0.216", "Potatoes");

1 Comment

Thank you, I had already noted and escaped the quotes as per your example. I will request that PS updates his answer to include that though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.