我想分別提取每個之間的文本###以與不同的檔案進行比較。需要提取所有CVEdocker 影像的所有數字,以便與之前的報告進行比較。檔案如下所示。這是一個片段,它有 100 多行這樣的行。需要通過 Shell 腳本執行此操作。請幫忙。
### Vulnerabilities found in docker image alarm-integrator:22.0.0-150
| CVE | X-ray Severity | Anchore Severity | Trivy Severity | TR |
| :--- | :------------: | :--------------: | :------------: | :--- |
|[CVE-2020-29361](#221fbde4e2e4f3dd920622768262ee64c52d1e1384da790c4ba997ce4383925e)|||Important|
|[CVE-2021-35515](#898e82a9a616cf44385ca288fc73518c0a6a20c5e0aae74ed8cf4db9e36f25ce)|||High|
### Vulnerabilities found in docker image br-agent:22.0.0-154
| CVE | X-ray Severity | Anchore Severity | Trivy Severity | TR |
| :--- | :------------: | :--------------: | :------------: | :--- |
|[CVE-2020-29361](#221fbde4e2e4f3dd920622768262ee64c52d1e1384da790c4ba997ce4383925e)|||Important|
|[CVE-2021-23214](#75eaa96ec256afa7bc6bc3445bab2e7c5a5750678b7cda792e3c690667eacd98)|||Important|
我已經嘗試過這樣的事情,grep -oP '(?<=\"##\").*?(?=\"##\")'但它不起作用。
預期輸出:
For alarm-integrator
CVE-2020-29361
CVE-2021-35515
For br-agent
CVE-2020-29361
CVE-2021-23214
uj5u.com熱心網友回復:
使用您顯示的示例,請嘗試以下awk代碼。
awk '
/^##/ && match($0,/docker image[[:space:]] [^:]*/){
split(substr($0,RSTART,RLENGTH),arr1)
print "for "arr1[3]
next
}
match($0,/^\|\[[^]]*/){
print substr($0,RSTART 2,RLENGTH-2)
}
' Input_file
說明:為上述awk代碼添加詳細說明。
awk ' ##Starting awk program from here.
/^##/ && match($0,/docker image[[:space:]] [^:]*/){ ##Checking condition if line starts from ## AND using match function to match regex docker image[[:space:]] [^:]* to get needed value.
split(substr($0,RSTART,RLENGTH),arr1) ##Splitting matched part in above match function into arr1 array with default delimiter of space here.
print "for "arr1[3] ##Printing string for space arr1 3rd element here
next ##next will skip all further statements from here.
}
match($0,/^\|\[[^]]*/){ ##using match function to match starting |[ till first occurrence of ] here.
print substr($0,RSTART 2,RLENGTH-2) ##printing matched sub string from above regex.
}
' Input_file ##mentioning Input_file name here.
uj5u.com熱心網友回復:
使用 GNU awk(我假設您已經擁有或可以獲得,因為您使用的是 GNU grep)作為匹配()的第三個引數:
$ cat tst.awk
match($0,/^###.* ([^:] ):.*/,a) { print "For", a[1] }
match($0,/\[([^]] )/,a) { print a[1] }
!NF
$ awk -f tst.awk file
For alarm-integrator
CVE-2020-29361
CVE-2021-35515
For br-agent
CVE-2020-29361
CVE-2021-23214
uj5u.com熱心網友回復:
awk你可以做:
awk -v FS=' |[[]|[]]' '/^[#] /{sub(/:.*$/,"");print "For " $NF} /^\|\[/{print $2} /^$/ {print ""}' file
For alarm-integrator
CVE-2020-29361
CVE-2021-35515
For br-agent
CVE-2020-29361
CVE-2021-23214
- 我們將欄位分隔符配置
FS為|[[]|[]]:空格或[字符或]字符。 - 第一個條件動作是獲取
For alarm-integrator和For br-agent - 第二個條件動作
all CVE numbers - 最后我們添加空行。
更具可讀性:
awk -v FS=' |[[]|[]]' '
/^[#] /{sub(/:.*$/,"");print "For " $NF}
/^\|\[/{print $2}
/^$/ {print ""}
' file
For alarm-integrator
CVE-2020-29361
CVE-2021-35515
For br-agent
CVE-2020-29361
CVE-2021-23214
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/410232.html
標籤:
