использование awk для объединения нескольких строк в однострочные записи при удалении некоторых данных - PullRequest
0 голосов
/ 04 марта 2020

У меня есть этот текстовый файл;

>> 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 $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, пожалуйста

Ответы [ 2 ]

0 голосов
/ 04 марта 2020

Вы бы попробовали следующее:

awk '
    sub(/.*Issue: \[/, "") {            # if $0 matches the substring, remove the leading portion
        sub(/:.*/, ""); str = $0        # and remove the trailing portion
    }
    sub(/.*Location: /, "") {
        print str, $0
    }' 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
0 голосов
/ 04 марта 2020

Одним из решений может быть:

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
...