0

I need to get 'test2' value from a json string in bash. The only way I found with the minimum of dependency is to use perl regexp pattern like this:

echo '{"test1":1119,"test2":1118 }' | grep -Po '"test2":\K\d+'
1118

However, How could I achieve grep with a variable ? The following wont work becase $param is within single quote. I can't find a way around this

param="test2"
echo '{"test1":1119,"test2":1118 }' | grep -Po '"$param":\K\d+'

Any help would be greatly appreciated

2
  • With some kind of concatenation maybe. Commented Mar 21, 2016 at 10:21
  • 2
    Put it inside double quotes. BTW, there's a tool called jq that can be used to parse JSON in bash. Commented Mar 21, 2016 at 10:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.