5

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?

vhs
  • 9,316
  • 3
  • 66
  • 70

1 Answers1

5

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 printing
  • the p 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 }
Sundeep
  • 23,246
  • 2
  • 28
  • 103
  • Nailed it. Thanks, Sundeep. Thoughts on making this portable to BSD sed? – vhs Aug 24 '18 at 09:39
  • 1
    I don't have BSD sed to test it, but I feel this should work on any sed... see https://stackoverflow.com/questions/24275070/sed-not-giving-me-correct-substitute-operation-for-newline-with-mac-difference/ for further help.. – Sundeep Aug 24 '18 at 10:05
  • 1
    The first of the two works on BSD, whereas the second results in empty output. Both work fine on BusyBox, which is what I asked for. I'll dig into the differences in sed using the relevant link you provided. Thanks again for your help. – vhs Aug 24 '18 at 10:32