我正在编写一个shell脚本来计算两个不同文件之间常用字的数量,而我无法弄清楚如何去做。唯一给出的是我必须使用grep
。
例如,如果我的第一个文件是:
egg
frog
horse
,第二个是:
dog
cat
egg
输出应为:1
答案 0 :(得分:0)
你可以这样做:
#!/bin/bash
words=`cat "file1"`
count=0
for word in $words; do
grep -q "$word" "file2" && ((count++))
done
echo "Number of match: $count"
输出:
Number of match: 1
file1
是file1的路径,file2
是file2的路径
答案 1 :(得分:0)
试试这个
grep -f file1 file2 | wc -l </ p>