Linux

替換引號內的特定字元

  • April 18, 2020

我試圖掩蓋日誌文件中的一些敏感數據。

我首先需要從文件中過濾掉具有匹配模式的特定行,然後對於那些特定的行,我需要替換雙引號內的任何文本,但不要留下任何不是的文本。

在該文件中,所有與模式匹配的行,包含雙引號的行,雙引號內的任何內容都需要替換為任何 AZ 被 X 替換,任何 az 被 x 替換,任何數字 0-9 被 0 替換。

在一行中,可以有多個帶引號的字元串。引號內也可以是特殊字元,例如“,”、“-”、“.”、“@”,這些應按原樣保留。

一個範例文件內容(在這種情況下過濾詞是’KEYWORD’):

2020-04-18 15:01:12 [EVENT] :log-event-with-KEYWORD: {:entry1 {:entry2 {:value "Replace This"}}} -> {:entry1 {:entry2 {:value "Replace ALSO this."}}}
2020-04-18 15:01:13 [EVENT] :log-event-with-KEYWORD: {:entry1 {:entry2 {:value "REplace. THIS 12345"}}}
2020-04-18 15:01:15 [EVENT] :this_has--the-KEYWORD: {:entry1 {:entry2 {:value "example.email@company.com"}}} -> {:entry1 {:entry2 {:value "email-address@mail.com"}}}
2020-04-18 15:01:18 [EVENT] :log-event-without-keyword: {:entry1 {:entry2 {:value "Do NOT replace this."}}} -> {:entry1 {:entry2 {:value "Do-NoT replace this either"}}}

該文件作為輸入將被處理成這個輸出:

2020-04-18 15:01:12 [EVENT] :log-event-with-KEYWORD: {:entry1 {:entry2 {:value "Xxxxxxx Xxxx"}}} -> {:entry1 {:entry2 {:value "Xxxxxxx XXXX xxxx."}}}
2020-04-18 15:01:13 [EVENT] :log-event-with-KEYWORD: {:entry1 {:entry2 {:value "XXxxxxx. XXXX 00000"}}}
2020-04-18 15:01:15 [EVENT] :this_has--the-KEYWORD: {:entry1 {:entry2 {:value "xxxxxxx.xxxxx@xxxxxxx.xxx"}}} -> {:entry1 {:entry2 {:value "xxxxx-xxxxxxx@xxxx.xxx"}}}
2020-04-18 15:01:18 [EVENT] :log-event-without-keyword: {:entry1 {:entry2 {:value "Do NOT replace this."}}} -> {:entry1 {:entry2 {:value "Do-NoT replace this either"}}}

需要在文件中更新更改的行,或者將具有這些修改的整個文件扔到標準輸出中(還有那些沒有關鍵字、行順序等的行,應保留詳細資訊。

這是否可以使用 bash 腳本/命令行工具(如 grep 和/或 sed)來完成?

awk '/KEYWORD/{
   n=split($0,a,"\"")
   for(i=2;i<=n;i=i+2){
       gsub(/[A-Z]/,"X",a[i])
       gsub(/[a-z]/,"x",a[i])
       gsub(/[0-9]/,"0",a[i])
   }
   sep=""
   for (i=1;i<=n;i++){
       printf "%s%s",sep,a[i]
       sep="\""
   }
   printf "\n"
   next
}
1' file

例如,在您更新的輸入文件上

2020-04-18 15:01:12 [EVENT] :log-event-with-KEYWORD: {:entry1 {:entry2 {:value "Replace This"}}} -> {:entry1 {:entry2 {:value "Replace ALSO this."}}}
2020-04-18 15:01:13 [EVENT] :log-event-with-KEYWORD: {:entry1 {:entry2 {:value "REplace. THIS 12345"}}}
2020-04-18 15:01:15 [EVENT] :this_has--the-KEYWORD: {:entry1 {:entry2 {:value "example.email@company.com"}}} -> {:entry1 {:entry2 {:value "email-address@mail.com"}}}
2020-04-18 15:01:18 [EVENT] :log-event-without-keyword: {:entry1 {:entry2 {:value "Do NOT replace this."}}} -> {:entry1 {:entry2 {:value "Do-NoT replace this either"}}}

此 awk 產生所需的輸出

2020-04-18 15:01:12 [EVENT] :log-event-with-KEYWORD: {:entry1 {:entry2 {:value "Xxxxxxx Xxxx"}}} -> {:entry1 {:entry2 {:value "Xxxxxxx XXXX xxxx."}}}
2020-04-18 15:01:13 [EVENT] :log-event-with-KEYWORD: {:entry1 {:entry2 {:value "XXxxxxx. XXXX 00000"}}}
2020-04-18 15:01:15 [EVENT] :this_has--the-KEYWORD: {:entry1 {:entry2 {:value "xxxxxxx.xxxxx@xxxxxxx.xxx"}}} -> {:entry1 {:entry2 {:value "xxxxx-xxxxxxx@xxxx.xxx"}}}
2020-04-18 15:01:18 [EVENT] :log-event-without-keyword: {:entry1 {:entry2 {:value "Do NOT replace this."}}} -> {:entry1 {:entry2 {:value "Do-NoT replace this either"}}}

引用自:https://unix.stackexchange.com/questions/580754