使用 awk 将多行合并为单行记录,同时删除一些数据

using awk to merge multiple lines into single line records while removing some of the data

我有这个文本文件;

>> Issue: [B608:hardcoded_sql_expressions] Possible SQL injection vector through string-based query construction.
   Severity: Medium   Confidence: Low
   Location: ./batch/action_reward.py:250
   More Info: https://bandit.readthedocs.io/en/latest/plugins/b608_hardcoded_sql_expressions.html
>> Issue: [B303:blacklist] Use of insecure MD2, MD4, MD5, or SHA1 hash function.
   Severity: Medium   Confidence: High
   Location: ./batch/local_runs/get_oapi_stores.py:33
   More Info: https://bandit.readthedocs.io/en/latest/blacklists/blacklist_calls.html#b303-md5
>> Issue: [B605:start_process_with_a_shell] Starting a process with a shell, possible injection detected, security issue.
   Severity: High   Confidence: High
   Location: ./batch/local_runs/get_oapi_stores.py:212
   More Info: https://bandit.readthedocs.io/en/latest/plugins/b605_start_process_with_a_shell.html

需要转换成单行记录,像这样;

B608 ./batch/action_reward.py:253
B303 ./batch/local_runs/get_oapi_stores.py:33
B605 ./batch/local_runs/get_oapi_stores.py:212

到目前为止,已经开始使用带有记录和字段分隔符的 awk

我用 "awk -f sort.awk filename"

执行
BEGIN { RS = ">>" ; FS = "\n" }

{
      print " "
}

接近但未完成...

^I$
^I$
 Issue: [B608:hardcoded_sql_expressions] Possible SQL injection vector through string-based query construction.^I   Location: ./batch/action_reward.py:253$
^I$
 Issue: [B303:blacklist] Use of insecure MD2, MD4, MD5, or SHA1 hash function.^I   Location: ./batch/local_runs/get_oapi_stores.py:33$
^I$
 Issue: [B605:start_process_with_a_shell] Starting a process with a shell, possible injection detected, security issue.^I   Location: ./batch/local_runs/get_oapi_stores.py:212$

如何从行中删除 headers / 剩余文本并删除它创建的额外空白行?

有没有办法用 substr 或等价物来做到这一点?

请使用 awk 回答

一个解决方案可能是:

awk '
 ==">>"{
   sub(/^\[/, "", ) # remove first `[`
   sub(/:.*/, "", ) # remove everything after `:`
   str=             # save  in variable `str`
 }
 =="Location:"{
   print str,       # print `str` and 
 }
' file

你会尝试以下方法吗:

awk '
    sub(/.*Issue: \[/, "") {            # if [=10=] matches the substring, remove the leading portion
        sub(/:.*/, ""); str = [=10=]        # and remove the trailing portion
    }
    sub(/.*Location: /, "") {
        print str, [=10=]
    }' file.txt

输出:

B608 ./batch/action_reward.py:250
B303 ./batch/local_runs/get_oapi_stores.py:33
B605 ./batch/local_runs/get_oapi_stores.py:212