Monday, 14 March 2022

Port Stripping

 So, like most things we do with tech, there's probably 73 different ways to achieve this particular requirement ...

However, I had a specific need to parse a JSON document, pull out a specific key, which contained an endpoint URL, and extract only the port number.

Here's an example of my JSON document: -

{

    "endpointURL":"https://www.ibm.com:443"

}

and, specifically, I just want to extract the port number of 443.

This is up with what I ended: -

cat server.json | jq -r '.endpointURL' | sed 's/.*://'

so it's three distinct commands, tied together with pipes: -

1. cat server.json to dump out the content of the document

2. jq -r '.endpointURL'  to only pull the endpointURL key

3. sed 's/.*://' to apply a regular expression ( aka RegExp ) to extract the port number

cat server.json | jq -r '.endpointURL' | sed 's/.*://'

443

Nice!

No comments:

Reminder - installing podman and skopeo on Ubuntu 22.04

This follows on from: - Lest I forget - how to install pip on Ubuntu I had reason to install podman  and skopeo  on an Ubuntu box: - lsb_rel...