I'm working on a blog theme for Hugo installable on Android (BusyBox via Termux) and plan to create a BusyBox Docker image and copy my theme and the hugo binary to it for use on ARM.
Theme releases are archived and made available on NPM and the tools available on BusyBox have allowed me to reliably parse version
from the metadata from JSON:
meta=$(wget -qO - https://registry.npmjs.org/package/latest)
vers=$(echo "$meta" | egrep -o "\"version\".*[^,]*," | cut -d ',' -f1 | cut -d ':' -f2 | tr -d '" ')
Now I would like to copy the dist
value from the meta
into a text file for use in Hugo:
"dist": {
"integrity": "sha512-3MH2/UKYPjr+CTC85hWGg/N3GZmSlgBWXzdXHroDfJRnEmcBKkvt1oiadN8gzCCppqCQhwtmengZzg0imm1mtg==",
"shasum": "a159699b1c5fb006a84457fcdf0eb98d72c2eb75",
"tarball": "https://registry.npmjs.org/after-dark/-/after-dark-6.4.1.tgz",
"fileCount": 98,
"unpackedSize": 5338189
},
Above pretty-printed for clarity. The actual metadata is compressed.
Is there a way I can reuse the version
parsing logic above to also pull the dist
field value?
Proper robust parsing requires tools like jq
where it could be as simple as jq '.version' ip.txt
and jq '.dist' ip.txt
You could use sed
but use it at your own risk
$ sed -n 's/.*"version":"\([^"]*\).*/\1/p' ip.txt
6.4.1
$ sed -n 's/.*\("dist":{[^}]*}\).*/\1/p' ip.txt
"dist":{"integrity":....
....}
-n
option to disable automatic printingp
modifier with s
command will allow to print only when substitution succeeds, this will mean output is empty instead of entire input line when something goes wrong.*"version":"\([^"]*\).*
this will match entire line, capturing data between double quotes after version
tag - you'll have to adjust the regex if whitespaces are allowed and other valid json formats.*\("dist":{[^}]*}\).*
this will match entire line, capturing data starting with "dist":{
and first occurrence of }
afterwards - so this is not suited if the tag itself can contain }