1

Using curl, I get some statistical information from my TP-Link router. I put that information to a text file. However, I need this info in JSON format so that I can use it elsewhere. The format of the text file is like this:

[0,0,0,0,0,0]0
enable=1
interval=10
action=0
[1,0,0,0,0,0]1
ipAddress=3232235848
macAddress=EC:0E:C4:4F:XX:XX
totalPkts=201
totalBytes=22914
currPkts=0
currBytes=0
currIcmp=0
currUdp=0
currSyn=0
currIcmpMax=0
currUdpMax=15
currSynMax=0
[2,0,0,0,0,0]1
ipAddress=3232235829
macAddress=00:16:E8:22:XX:XX
totalPkts=972
totalBytes=98730
currPkts=0
currBytes=0
currIcmp=0
currUdp=0
currSyn=0
currIcmpMax=0
currUdpMax=14
currSynMax=4
[3,0,0,0,0,0]1
ipAddress=3232235842
macAddress=70:14:A6:8B:XX:XX
totalPkts=154566
totalBytes=15116490
currPkts=0
currBytes=0
currIcmp=0
currUdp=0
currSyn=0
currIcmpMax=9
currUdpMax=58
currSynMax=60

I need a JSON something like:

{"ipAddress" : "3232235848",
"macAddress" : "EC:0E:C4:4F:XX:XX",
"totalPkts" : "201",
"totalBytes" : "22914",
"currPkts" : "0",
"currBytes" : "0",
"currIcmp" : "0",
"currUdp" : "0",
"currSyn" : "0",
"currIcmpMax" : "0",
"currUdpMax" : "15",
"currSynMax" : "0"}
{"ipAddress" : "3232235829", ...

Could you show me the most efficient way of doing this? I prefer a bash script if it is possible? Tried putting "| jq -R -s -c 'split("\n")'" to the end of curl command but then it looks like: {"ipAddress=3232235848","macAddress=EC:0E:C4:4F:XX:XX" ...

  • bash is just your shell, not a text editor – Jeff Schaller Jun 4 '17 at 15:04
  • It looks like you created a second account and then tried to say thank you and ask a second question. Please accept an answer to this question before asking a separate one. – Jeff Schaller Jun 5 '17 at 9:54
1
perl -lne 'BEGIN{*1 = sub {print q<{>, join(",\n", splice @A, 0, @A), q<}>}}
   next if $. == 1;
   /^\[(?:\d+,?)+\]\d+$/ and &1,next;
   push @A, join q/ : /, map qq/"$_"/, split /=/;
   eof && &1;
' yourfile
0

sed hack:

sed -E -e 's/\[([0-9],){5}[0-9]\][0-9]+/}{/g; s/([[:alnum:]]+)=([[:alnum:]]+)/"\1" : "\2",/; 
           s/,\n}/}/;  1s/.*/{/; $s/.*/&}/' yourfile | sed -Ez 's/,\n?}/ }\n/g'

The output:

{
"enable" : "1",
"interval" : "10",
"action" : "0" }
{
"ipAddress" : "3232235848",
"macAddress" : "EC",:0E:C4:4F:XX:XX
"totalPkts" : "201",
"totalBytes" : "22914",
"currPkts" : "0",
"currBytes" : "0",
"currIcmp" : "0",
"currUdp" : "0",
"currSyn" : "0",
"currIcmpMax" : "0",
"currUdpMax" : "15",
"currSynMax" : "0" }
{
"ipAddress" : "3232235829",
"macAddress" : "00",:16:E8:22:XX:XX
"totalPkts" : "972",
"totalBytes" : "98730",
"currPkts" : "0",
"currBytes" : "0",
"currIcmp" : "0",
"currUdp" : "0",
"currSyn" : "0",
"currIcmpMax" : "0",
"currUdpMax" : "14",
"currSynMax" : "4" }
{
"ipAddress" : "3232235842",
"macAddress" : "70",:14:A6:8B:XX:XX
"totalPkts" : "154566",
"totalBytes" : "15116490",
"currPkts" : "0",
"currBytes" : "0",
"currIcmp" : "0",
"currUdp" : "0",
"currSyn" : "0",
"currIcmpMax" : "9",
"currUdpMax" : "58",
"currSynMax" : "60" }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.