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

问题描述 投票:0回答:1

我有这个文本文件;

>> 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文件名执行]

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

{
      print $1" "$3
}

接近但不完整...

^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$

我如何从行中删除标题/剩余文本并删除其创建的多余空白行?

有没有办法使用substr或等效方法?

请使用awk回答

bash awk merge record
1个回答
0
投票

一个解决方案可能是:

awk '
 $1==">>"{
   sub(/^\[/, "", $3) # remove first `[`
   sub(/:.*/, "", $3) # remove everything after `:`
   str=$3             # save $3 in variable `str`
 }
 $1=="Location:"{
   print str,$2       # print `str` and $2
 }
' file
© www.soinside.com 2019 - 2024. All rights reserved.