我想撰寫一個腳本來屏蔽 xml 日志檔案中的部分資料。我的方法是 - 將有一個查找檔案,我可以在其中添加需要用“是”或“否”標志屏蔽的標簽。我的 unix 腳本將讀取查找檔案并僅屏蔽那些具有“是”標志的標簽。我在我的腳本中使用 awk 從 xml 日志檔案中查找標記(在查找檔案中具有 Yes 標志)并將其值替換為模式。xml 檔案未在日志檔案中決議。我只需要屏蔽 xml 標記中存在的值。
非常感謝任何更改代碼或我的方法的建議。
日志檔案中的 XML
2022-02-08 [this is the actual log file] BLAH - This is how my xml log file is <sometag id="00000-00000"<name>sam</name><phone>98762123</phone><DOB>12-09-77</DOB><bankaccount>4563728495847</bankaccount></sometag>
2022-02-09 [this is the actual log file] BLAH - This is how my xml log file is <sometag id="00000-00000"<name>sam</name><phone>123456789</phone><DOB>12-09-77</DOB><bankaccount>4563728495847</bankaccount></sometag>
2022-02-08 [this is the actual log file] BLAH
2022-02-08 [this is the actual log file] BLAH
2022-02-08 [this is the actual log file] BLAH
查找檔案
tag, flag
name,Yes
phone,Yes
DOB,No
輸出檔案必須如下所示
2022-02-08 [this is the actual log file] BLAH - This is how my xml log file is <sometag id="00000-00000"<name>s**</name><phone>9**6**2*</phone><DOB>**-**-**</DOB><bankaccount>4**37**4**8**</bankaccount></sometag>
2022-02-09 [this is the actual log file] BLAH - This is how my xml log file is <sometag id="00000-00000"<name>s**</name><phone>9**6**2*</phone><DOB>12-09-77</DOB><bankaccount>4**37**4**8**</bankaccount></sometag>
2022-02-08 [this is the actual log file] BLAH
2022-02-08 [this is the actual log file] BLAH
2022-02-08 [this is the actual log file] BLAH
PS:屏蔽演算法必須根據某種模式屏蔽值。PPS:使用 awk 決議 xml 不是我知道的最佳選擇,但我在這里沒有選擇。
uj5u.com熱心網友回復:
POSIX awk:
$ cat mask-list
name 1
phone 10010010
DOB 00100100
bankaccount 1001100100100
$ cat mask.sh
awk '
function mask(str, str_masked) {
for (j=1; j<=length(str); j ) {
if (substr(masks[i], j, 1)==1) {
c = substr(str, j, 1)
} else {
c = "*"
}
str_masked = str_masked c
}
return str_masked
}
FNR == NR {
tags[NR-1] = $1
masks[NR-1] = $2
}
FNR != NR {
line = $0
for (i in tags) {
regex = "<"tags[i]">[^<] </"tags[i]">"
masked_line = ""
l = length(tags[i])
while (match(line, regex) > 0) {
# extract tag value and mask it
fulltag = substr(line, RSTART, RLENGTH)
tagval = substr(fulltag, l 3, RLENGTH-l-l-5)
fulltag_masked = "<"tags[i]">" mask(tagval) "</"tags[i]">"
# append the line portion before tag, and the masked tag
masked_line = masked_line substr(line, 1, RSTART-1) fulltag_masked
# truncate line start to end of matched tag, for next match
line = substr(line, RSTART RLENGTH)
}
line = masked_line line
}
print line
}' "$@"
例子:
$ sh mask.sh mask-list log-file
2022-02-08 [this is the actual log file] BLAH - This is how my xml log file is <sometag id="00000-00000"<name>s**</name><phone>9**6**2*</phone><DOB>**-**-**</DOB><bankaccount>4**37**4**8**</bankaccount></sometag>
2022-02-09 [this is the actual log file] BLAH - This is how my xml log file is <sometag id="00000-00000"<name>s**</name><phone>1**4**7**</phone><DOB>**-**-**</DOB><bankaccount>4**37**4**8**</bankaccount></sometag>
2022-02-08 [this is the actual log file] BLAH
2022-02-08 [this is the actual log file] BLAH
2022-02-08 [this is the actual log file] BLAH
解釋:
- 閱讀掩碼檔案以獲取要掩碼的標簽及其掩碼圖案。
- 對于日志檔案,使用
match()andsubstr()獲取<tag>val</tag>變數中的每個相關子字串,然后substr()再次獲取 getval,因此可以根據當前標簽的模式將其傳遞給屏蔽它的函式。 - 為每個標簽重新組裝并重復。
- 掩碼串列包括對應的掩碼圖案。
1表示顯示,任何其他字符(或無,見name)表示隱藏。您可以添加更多條目。 - 在
mask()函式中,有一個未使用的引數str_masked,用于保持該變數的本地范圍。 substr()用于一次比較一個字符的字串和掩碼模式。
uj5u.com熱心網友回復:
使用 GNU awk 將 3rg arg 用于 match() 和 gensub():
$ cat tst.awk
BEGIN { FS="," }
NR==FNR {
if ( $2 == "Yes" ) {
mask[$1]
}
next
}
match($0,"(.*<sometag[^<>] )(.*)(</sometag>.*)",rec) {
bef = rec[1]
xml = rec[2]
aft = rec[3]
$0 = bef
while ( match(xml,"(<([^>] )>)([^<]*)(<[^>] >)",tagVals) ) {
tag = tagVals[2]
if ( tag in mask ) {
val = tagVals[3]
masked = gensub(/(.)../,"\\1**","g",val" ")
tagVals[3] = substr(masked,1,length(val))
}
$0 = $0 tagVals[1] tagVals[3] tagVals[4]
xml = substr(xml,tagVals[0,"length"])
}
$0 = $0 aft
}
{ print }
$ awk -f tst.awk lookup.txt file
2022-02-08 [this is the actual log file] BLAH - This is how my xml log file is <sometag id="00000-00000"<name>s**</name><phone>9**6**2*</phone><DOB>12-09-77</DOB><bankaccount>4563728495847</bankaccount></sometag>
2022-02-09 [this is the actual log file] BLAH - This is how my xml log file is <sometag id="00000-00000"<name>s**</name><phone>1**4**7**</phone><DOB>12-09-77</DOB><bankaccount>4563728495847</bankaccount></sometag>
2022-02-08 [this is the actual log file] BLAH
2022-02-08 [this is the actual log file] BLAH
2022-02-08 [this is the actual log file] BLAH
以上假設您發布的預期輸出是錯誤的,并且您并不真正期望 DOB 或銀行帳戶值會發生變化。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/430486.html
上一篇:使用lxml在python中將一個xml復制到另一個
下一篇:在PHP中將字串轉換為xml
