bash在一个文件中搜索第二个文件的内容

时间:2019-01-08 16:39:57

标签: bash shell grep

我有两个文件,“ word123.txt”每行包含三个单词(word1 word2 word3),我有“ word45.txt”,每行包含2个单词(word4 word5)。我需要针对“ word45.txt”的每个“ word4 word5”组合/词组搜索“ word123.txt”,无论大小写如何。这就是我所拥有的,但是由于某些原因它没有实现。

从“ word123.txt”中采样数据

Cherry Bunny 564r789
Sonic hedgehog 84125d6
Krispy Kreme 24rt852

要在“ word123.txt”中搜索的来自“ word45.txt”的样本数据

Harry Henderson
veruca salt
sonic hedgehog
Larry david

这将返回“音速刺猬”的匹配项。这是我想出的,但是不起作用:

mapfile -t wordsToSearch < word45.txt

counter=0; 
while  [ $counter -lt $( wc -l < word45.txt ) ]; 
do cat word123.txt | grep -i "^${wordsToSearch[$counter]}" >> matches.txt;
let counter=counter+1;
done;

我还尝试了this post中的解决方案(针对我的目的进行了修改):

cat word123.txt | grep -f word45.txt

在此先感谢您的帮助。

0 个答案:

没有答案