我不熟悉 bash 腳本,經過一些研究,我找到了一些提示,但仍然需要您的努力。
鑒于我有一個resources.txt,包含
a
b
c
d
和一個whitelist.txt檔案,包含
c
d
我想洗掉從白名單檔案到資源檔案完全匹配的所有專案。所以預期的輸出是
a
b
期望 c 和 d 被洗掉,因為它們在白名單檔案中。
我創建了下面的腳本來閱讀它,但不知道如何將每個腳本一一替換為資源檔案。
# read the whitelist file
echo whitelist.txt | awk '{for (i=1; i<=NF; i ) printf "%s\n",$i}
# replace item in resource file
awk '{sub(/c/,""); print}' resources.txt
非常感謝您的幫助,非常感謝!!
uj5u.com熱心網友回復:
我會使用這個grep:
grep -Fvxf whitelist.txt resources.txt
-F固定/文字字串(無正則運算式)-f FILE從檔案中獲取模式-x匹配整行-v列印不匹配的行- 這個grep是POSIX
uj5u.com熱心網友回復:
使用 awk 你可以使用
awk 'FNR==NR{a[$0];next}!($0 in a)' whitelist.txt resources.txt
在零件中
awk '
FNR==NR{ # Only for the first file (whitelist.txt)
a[$0] # Set the value of the whole line as a key in a
next # Go to the next record
}
!($0 in a) # Print the line if the value of `$0` does not exists as array index in a
' whitelist.txt resources.txt
輸出
a
b
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/368386.html
