Reputation: 3198
I am attempting to parse the nodejs package.json for the version number.. this line usually works but somehow I am now getting an error of invalid block
PACKAGE_VERSION=$(cat package.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[",]//g' | xargs)
specifically this area is highlighted in VSCode sed 's/......
How can I repair this line? is the "
needing escaping or ?
Upvotes: 0
Views: 80
Reputation: 6357
The problem is YAML-related (it seems the combination of :
and |
is the culprit).
The easy fix is to use a YAML block:
script:
# …
- |-
PACKAGE_VERSION=$(cat package.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[",]//g' | xargs)
And while you’re at it you can use a folding block to make it more readable:
script:
# …
- >-
PACKAGE_VERSION=$(
cat package.json |
grep version |
head -1 |
awk -F: '{ print $2 }' |
sed 's/[",]//g' |
xargs
)
Chaining grep
, head
, awk
and sed
can usually be replaced by a single awk
. And there’s a useless use of cat
. Not to mention parsing JSON without a proper JSON parser.
Upvotes: 3