Home > Blockchain >  how to fetch a selected value from a text file using a shell script
how to fetch a selected value from a text file using a shell script

Time:09-16

I have a file which looks like this (myfile.txt)

{"projectStatus":{"status":"ERROR","conditions":[{"status":"ERROR","metricKey":"new_bugs","comparator":"GT","periodIndex":1,"errorThreshold":"0","actualValue":"2"},
{"status":"ERROR","metricKey":"new_reliability_rating","comparator":"GT","periodIndex":1,"errorThreshold":"1","actualValue":"3"},
{"status":"ERROR","metricKey":"new_security_rating","comparator":"GT","periodIndex":1,"errorThreshold":"1","actualValue":"2"},
{"status":"OK","metricKey":"new_maintainability_rating","comparator":"GT","periodIndex":1,"errorThreshold":"1","actualValue":"1"},
{"status":"ERROR","metricKey":"new_code_smells","comparator":"GT","periodIndex":1,"errorThreshold":"0","actualValue":"17"},
{"status":"ERROR","metricKey":"new_vulnerabilities","comparator":"GT","periodIndex":1,"errorThreshold":"0","actualValue":"2"},
{"status":"ERROR","metricKey":"coverage","comparator":"LT","errorThreshold":"90","actualValue":"80.3"},
{"status":"ERROR","metricKey":"new_coverage","comparator":"LT","periodIndex":1,"errorThreshold":"90","actualValue":"7.826086956521739"}],
"periods":[{"index":1,"mode":"previous_version","date":"2021-11-04T14:47:41 0000"}],"ignoredConditions":false}}

I have to pick "actualValue":"7.826086956521739" which is with "metricKey":"new_coverage"

then expected output is (actualValue)

7.826086956521739

this is what I tried

sed -n 's/"metricKey":"new_coverage" "actualValue": //p' myfile.txt 

Can someone help me to figure out this? Thanks in advance!

Note: I am not allowed to use jq or a general purpose scripting language (JavaScript, Python etc).

CodePudding user response:

$ sed -n 's/.*"metricKey":"new_coverage".*"actualValue":"\([^"]*\)".*/\1/p' file
7.826086956521739

CodePudding user response:

Here is another simple solution:

sed -rn 's/^.*"actualValue":"(.*)".*$/\1/p' file.txt
  • Related