tooptoop4
tooptoop4

Reputation: 330

why is awk pattern match not returning results?

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

Answers (3)

vintnes
vintnes

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

sjsam
sjsam

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

tink
tink

Reputation: 15206

That will be because \s doesn't match?

Upvotes: 3

Related Questions