Home > database >  How fetch a value from a file with given conditions (using a shell script)
How fetch a value from a file with given conditions (using a shell script)

Time:10-05

I have to read a file (myfile.txt) and fetch the particular value matching with conditions and writing to a new file (newfile.txt).

I have to write "actualValue" matching with "status":"ERROR" , "metricKey":"new_coverage" to newfile.txt only if conditions are matched. otherwise I have to write newfile.txt empty.

if 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":"20"},{"status":"ERROR","metricKey":"new_vulnerabilities","comparator":"GT","periodIndex":1,"errorThreshold":"0","actualValue":"2"},{"status":"ERROR","metricKey":"coverage","comparator":"LT","errorThreshold":"90","actualValue":"80.9"},{"status":"ERROR","metricKey":"new_coverage","comparator":"LT","periodIndex":1,"errorThreshold":"90","actualValue":"14.810126582278482"}],"periods":[{"index":1,"mode":"previous_version","date":"2021-11-04T14:47:41 0000"}],"ignoredConditions":false}}

expected output on newfile.txt:

14.810126582278482

if myfile.txt

{"projectStatus":{"status":"ERROR","conditions":[{"status":"OK","metricKey":"new_bugs","comparator":"GT","periodIndex":1,"errorThreshold":"0","actualValue":"0"},{"status":"OK","metricKey":"new_reliability_rating","comparator":"GT","periodIndex":1,"errorThreshold":"1","actualValue":"1"},{"status":"OK","metricKey":"new_security_rating","comparator":"GT","periodIndex":1,"errorThreshold":"1","actualValue":"1"},{"status":"OK","metricKey":"new_maintainability_rating","comparator":"GT","periodIndex":1,"errorThreshold":"1","actualValue":"1"},{"status":"OK","metricKey":"new_code_smells","comparator":"GT","periodIndex":1,"errorThreshold":"0","actualValue":"0"},{"status":"OK","metricKey":"new_vulnerabilities","comparator":"GT","periodIndex":1,"errorThreshold":"0","actualValue":"0"},{"status":"ERROR","metricKey":"coverage","comparator":"LT","errorThreshold":"90","actualValue":"19.7"},{"status":"OK","metricKey":"new_coverage","comparator":"LT","periodIndex":1,"errorThreshold":"90","actualValue":"100.0"}],"periods":[{"index":1,"mode":"previous_version","date":"2022-06-29T05:54:05 0100"}],"ignoredConditions":false}}

expected output on newfile.txt:

empty - (I have to keep newfile.txt empty)

this is what I tried

sed -n 's/.*"status":"ERROR".*"metricKey":"new_coverage".*"actualValue":"\([^"]*\)".*/\1/p' myfile.txt > newfile.txt

I referred:

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

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

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

CodePudding user response:

With GNU grep you could try following code, written and tested with your shown samples. This will make sure your to print values of actualValue only if status is ERROR and metricKey is "new_coverage

grep -oP '"status":"ERROR","metricKey":"new_coverage".*?"actualValue":"\K([^"]*)' Input_file

CodePudding user response:

Using sed

$ sed -Ee '/.*status":"ERROR","metricKey":"new_coverage([^:]*:){4}"([^"]*).*/ ! d ;{s//\2/;wnewfile.txt' -e '}' input_file
14.810126582278482
$ cat newfile.txt
14.810126582278482

If the match is not found, the line is deleted and an empty file is written instead

  • Related