计算两个不同文件中的常用字数

时间:2015-04-02 06:38:01

标签: linux file shell grep

我正在编写一个shell脚本来计算两个不同文件之间常用字的数量,而我无法弄清楚如何去做。唯一给出的是我必须使用grep

例如,如果我的第一个文件是:

egg
frog
horse

,第二个是:

dog
cat
egg

输出应为:1

2 个答案:

答案 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>