我正在嘗試執行以下操作。
我有一個名為 checker.txt 的檔案,它包含以下內容
blue
brown
cat
dog
jumps
over
the
red
yellow
fox
quick
lazy
我有字串 the quick brown fox jumps over the lazy dog
這個想法是我想將字串 grep 到 checker.txt 檔案中。我想檢查一下“the quick brown fox jumps over the lazy dog”中的每個單詞是否在 checker.txt 檔案中。并回傳類似 1 的內容以確認字串中的所有單詞都在 checker.txt 檔案中。
我對 grep 真的很陌生,不知道這是否可能。
任何和所有幫助將不勝感激。
更多資訊:
我意識到你可以做這樣的事情,它允許你在一個檔案中準確地搜索多個單詞,比如:
grep -w 'the$\|quick$\|brown$' checker.txt
它回傳所有完全匹配的單詞。
然而,這意味著將原始字串更改為不同的形式。我可以這樣做,但我想找到是否有比決議它更簡單、更容易的替代方法。
uj5u.com熱心網友回復:
鑒于這兩個檔案:
cat checker.txt
blue
brown
cat
dog
jumps
over
the
red
yellow
fox
quick
lazy
cat file
the quick brown fox jumps over the lazy dog
像awk這樣的東西更容易:
awk 'FNR==NR{check[$1]; next} # read the checker file
# next file
{for (i=1;i<=NF;i ) # for each field in line
if (!($i in check)) next} # skip printing if word
# not in check file
1 ' checker.txt file # 1 mean 'print' in this case
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/341779.html
下一篇:從使用awk的第n列讀取
