[Cialug] Regex Question
Mike Hughes
mike at visionary.com
Mon Jul 25 17:16:39 UTC 2022
Yes, jq is very useful for parsing JSON files. Here's a snippet of a tool that I run on a couple Enterprise Linux clients that use ncat to monitor port availability for a set of targets:
mydata.json:
[
{
"serviceName":"THIS_SERVICE-00",
"localIp":"172.16.45.17",
"remoteIp":"192.168.154.26",
"remotePort":"456"
},
{
"serviceName":"THAT_SERVICE-01",
"localIp":"172.16.45.18",
"remoteIp":"10.11.12.13",
"remotePort":"789"
},
{
"serviceName":"ANOTHER_SERVICE-02",
"localIp":"172.16.45.19",
"remoteIp":"10.254.35.96",
"remotePort":"987"
}
]
parse_json.sh:
DATASET="mydata.json"
LOOP=`jq '. | length' ${DATASET}`
let "LOOP=${LOOP}-1"
for i in $(seq 0 ${LOOP})
do
#echo ${i}
SERVICE_NAME=`jq -r .[$i].serviceName ${DATASET}`
LOCAL_IP=`jq -r .[$i].localIp ${DATASET}`
REMOTE_IP=`jq -r .[$i].remoteIp ${DATASET}`
REMOTE_PORT=`jq -r .[$i].remotePort ${DATASET}`
nc -w 1 -z ${REMOTE_IP} ${REMOTE_PORT} ; SUCCESS=$?
if [ ${SUCCESS} -ne 0 ]
then
echo "Connection for ${SERVICE_NAME}, from ${LOCAL_IP} to ${REMOTE_IP}:${REMOTE_PORT}, is down."
else
echo "Connection for ${SERVICE_NAME} is up."
fi
done
________________________________
From: Cialug <cialug-bounces at cialug.org> on behalf of Dave Hala <dave at 58ghz.net>
Sent: Monday, July 25, 2022 10:43 AM
To: Central Iowa Linux Users Group <cialug at cialug.org>
Subject: Re: [Cialug] Regex Question
Wow!
I didn't know about jq... That looks like a *super* useful tool.
On Mon, Jul 25, 2022 at 10:40 AM Scott Yates <Scott at yatesframe.com> wrote:
> Yes, regex is probably not the right tool for that. jq would be better, or
> even just a little script, but trying to parse json with regex is asking
> for serious headaches.
>
> On Mon, Jul 25, 2022 at 10:18 AM Will <staticphantom at gmail.com> wrote:
>
> > I wouldn't use regex for this but I would use jq and pluck the fields you
> > want. Alternatively use grep -v and omit the line command line.
> >
> > -Will C
> >
> > On Mon, Jul 25, 2022, 11:15 Todd Walton <tdwalton at gmail.com> wrote:
> >
> > > Let's say I have a block of text like this:
> > >
> > > ===================================
> > > {
> > > "altGrAliasing": true,
> > > "antialiasingMode": "grayscale",
> > > "closeOnExit": "graceful",
> > > "colorScheme": "Campbell",
> > > "commandline":
> > >
> > >
> >
> "C:\\Users\\seven\\AppData\\Roaming\\Scoop\\apps\\msys2\\current\\msys2_shell.cmd
> > > -defterm -here -no-start -msys",
> > > "cursorShape": "bar",
> > > },
> > > ===================================
> > >
> > > How do I write a regex to match all lines *except* the "commandline"
> > line?
> > >
> > > --
> > > Todd
> > > _______________________________________________
> > > Cialug mailing list
> > > Cialug at cialug.org
> > > https://www.cialug.org/cgi-bin/mailman/listinfo/cialug
> > >
> > _______________________________________________
> > Cialug mailing list
> > Cialug at cialug.org
> > https://www.cialug.org/cgi-bin/mailman/listinfo/cialug
> >
> _______________________________________________
> Cialug mailing list
> Cialug at cialug.org
> https://www.cialug.org/cgi-bin/mailman/listinfo/cialug
>
--
NIFCAP -The Premier Client Intake System for Non-Profit Organizations.
https://www.osis.us
_______________________________________________
Cialug mailing list
Cialug at cialug.org
https://www.cialug.org/cgi-bin/mailman/listinfo/cialug
More information about the Cialug
mailing list