Hi friends! I'm no Perl expert but am trying to parse a file for some information which I would like to use to output some JSON formatted text for consumption by Zabbix. The file I'm parsing contains a derivative of the following text:
My Perl script looks as follows:#Bob monitoring cd /opt/bob-monitor ### version 2 java -jar libs/BobMonitor.jar -xruntime/bob311_2_acc_list.xml -l/tmp/b +ob311/fail.logs -s/tmp/bob311/results.txt 2>&1 java -jar libs/BobMonitor.jar -xruntime/bob411_2_acc_list.xml -l/tmp/b +ob411/fail.logs -s/tmp/bob411/results.txt 2>&1 ### version 3 java -jar libs/BobMonitor.jar -xruntime/bob312_3_acc_list.xml -l/tmp/b +ob312/fail.logs -s/tmp/bob312/results.txt 2>&1 java -jar libs/BobMonitor.jar -xruntime/bob412_3_acc_list.xml -l/tmp/b +ob412/fail.logs -s/tmp/bob412/results.txt 2>&1 ### version 4 java -jar libs/BobMonitor.jar -xruntime/bob313_4_acc_list.xml -l/tmp/b +ob313/fail.logs -s/tmp/bob313/results.txt 2>&1 java -jar libs/BobMonitor.jar -xruntime/bob413_4_acc_list.xml -l/tmp/b +ob413/fail.logs -s/tmp/bob413/results.txt 2>&1 ....
The desired output is something like the following:#!/usr/bin/perl print "{"; print " \"data\":\n\t[\n"; my $filename = '/opt/scripts/fester_monitor.sh'; open(my $fh, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!"; while (my $row = <$fh>) { chomp($row); for($row) { s/^cd.*//; s/^#.*//; ($festerinstance) = m/(fester(\d+))/; print ",\n"; $return = "\t{ \"{#FESTERINSTANCE}\":\"$festerinstance +\"}" unless /(^\s*$|^\,$)/; print $return; } } print "\t]\n"; print "}\n";
Yet, I'm getting this:{ "data": [ { "{#BOBINSTANCE}":"bob311"}, { "{#BOBINSTANCE}":"bob411"}, { "{#BOBINSTANCE}":"bob411"}, { "{#BOBINSTANCE}":"bob411"}, { "{#BOBINSTANCE}":"bob312"}, { "{#BOBINSTANCE}":"bob412"}, { "{#BOBINSTANCE}":"bob412"}, { "{#BOBINSTANCE}":"bob412"}, { "{#BOBINSTANCE}":"bob313"} .... ] }
The issue is the extra commas at the top of the output- which will not be well-formed JSON. Looking at my code, can any one of you perhaps spot something that I may be doing wrong?{ "data": [ , , , , , , { "{#BOBINSTANCE}":"bob311"}, { "{#BOBINSTANCE}":"bob411"}, { "{#BOBINSTANCE}":"bob411"}, { "{#BOBINSTANCE}":"bob411"}, { "{#BOBINSTANCE}":"bob312"}, { "{#BOBINSTANCE}":"bob412"}, { "{#BOBINSTANCE}":"bob412"}, { "{#BOBINSTANCE}":"bob412"}, { "{#BOBINSTANCE}":"bob313"} .... ] }
In reply to Problem printing JSON type output by jonathan.vanderwatt
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |