我必須讀取一個檔案(myfile.txt)并獲取與條件匹配的特定值并寫入一個新檔案(newfile.txt)。
僅當條件匹配時,我才必須將"actualValue"與"status":"ERROR",匹配寫入 newfile.txt 。"metricKey":"new_coverage"否則我必須將 newfile.txt 寫為空。
如果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}}
newfile.txt 的預期輸出:
14.810126582278482
如果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}}
newfile.txt 的預期輸出:
empty- (我必須保持 newfile.txt 為空)
這就是我嘗試過的
sed -n 's/.*"status":"ERROR".*"metricKey":"new_coverage".*"actualValue":"\([^"]*\)".*/\1/p' myfile.txt > newfile.txt
我提到:
如何使用 shell 腳本從文本檔案中獲取選定的值
有人可以幫我解決這個問題嗎?提前致謝!
注意:我不允許使用 Jq 或通用腳本語言(JavaScript、Python 等)。
uj5u.com熱心網友回復:
使用 GNU grep,您可以嘗試使用顯示的示例撰寫和測驗以下代碼。這將確保您actualValue僅列印 if statusisERROR和metricKeyis 的值"new_coverage
grep -oP '"status":"ERROR","metricKey":"new_coverage".*?"actualValue":"\K([^"]*)' Input_file
uj5u.com熱心網友回復:
使用sed
$ sed -Ee '/.*status":"ERROR","metricKey":"new_coverage([^:]*:){4}"([^"]*).*/ ! d ;{s//\2/;wnewfile.txt' -e '}' input_file
14.810126582278482
$ cat newfile.txt
14.810126582278482
如果未找到匹配項,則洗掉該行并寫入一個空檔案
uj5u.com熱心網友回復:
如果沒有嵌套的卷曲,您可以匹配開頭的卷曲并使用否定字符類匹配除卷曲以外的任何字符,[^{}]*直到到達actualValue,然后在組 1 中捕獲該值。
該模式可能如下所示:
.*{"status":"ERROR","metricKey":"new_coverage"[^{}]*"actualValue":"([^"{}]*)".*
正則運算式演示
示例在sed
sed -n 's/.*{"status":"ERROR","metricKey":"new_coverage"[^{}]*"actualValue":"\([^"{}]*\)".*/\1/p' file
輸出
14.810126582278482
uj5u.com熱心網友回復:
使用 sed 的最佳方法
sed -Ee '/.*status":"ERROR","metricKey":"new_coverage([^:]*:)
{4}"([^"]*).*/ ! d ;{s//\2/;file.txt' -e '}' log_file
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/512839.html
標籤:linux壳awksed
上一篇:我在運行此bash腳本時遇到問題
