Reputation: 330
create a file (x) with below 1 line:
{"dagId":"blashj","execDate":"20190426","status":"running","isActive":true}
--> returns desired value ---> true
cat x | sed -e 's/[{}"]/''/g' | awk -v RS=',' -F: '/^\isActive/ {print $0}'
--> why does this not return a value?? desired value ---> running
cat x | sed -e 's/[{}"]/''/g' | awk -v RS=',' -F: '/^\status/ {print $0}'
env is RHEL7.4, GNU Awk 4.0.2
Upvotes: 2
Views: 315
Reputation: 2030
If, like me, you don't have access to jq
at work, I would use a regular expression as the awk
Record Separator.
$ awk -F: -v RS='[{},]' '/"isActive"/{print $2}' x
true
$ awk -F: -v RS='[{},]' '/"status"/{print $2}' x
"running"
Upvotes: 1
Reputation: 21955
I would wholeheartedly endorse jq
which should be available on your platform :
jq -r '"isActive:"+(.isActive | tostring)' file
If you just need that value of an object attribute, it can be as simple as
jq -r '.status' file
The -r
or --raw-output
writes the string directly to standard output rather than being formatted as a JSON string with quotes
See jq is sed for json. Using the right tool saves time.
Upvotes: 1