如何将一个文件中的所有单词提供给tr
以便从另一文件中搜索和删除文本?
例如,我有一个文件vocabulary.txt
和loveStroty.txt
。我正在尝试从爱情故事中删除所有词汇。
$ voc="one free" #files look like this strings
$ love="one two free four"
$ tr "$voc" '' <<< $love
输出示例(与分隔符或换行符无关)
two
four
答案 0 :(得分:3)
我假设您的输入文件如下:
$ cat lovestory.txt
one two free four
$ cat vocabulary.txt
one free
然后在Bash中,我可以使用grep
,过程替换和tr
从lovestory.txt
中存在的vocabulary.txt
中删除每个单词,如下所示:
$ grep -vFxf <(tr ' ' '\n' < vocabulary.txt) <(tr ' ' '\n' < lovestory.txt)
two
four
tr ' ' '\n' < file
用换行符替换file
中的每个空格; grep -vFx
删除完整行的匹配项(固定字符串,无正则表达式)。
答案 1 :(得分:1)
如果文件不够大,可以尝试sed
实用程序:
# Define the text which replaces the searched words
replace="<Replacement string here>"
for word in $(cat /path/to/<file_containing_words>); do
sed -i "s/${word}/${replace}/g" <file_to_be_replaced>
done
因此,对于您的具体示例
replace=""
for word in $(cat /path/to/voc); do
sed -i "s/${word}/${replace}/g" /path/to/love
done
答案 2 :(得分:0)
使用GNU awk进行多字符RS:
$ awk -v RS='\\s+' 'NR==FNR{a[$0];next} !($0 in a)' vocabulary.txt lovestory.txt
two
four