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

Counting the number of common words in two different files

我正在编写一个 shell 脚本来计算两个不同文件之间的常用词数量,但我不知道该怎么做。唯一给出的是我必须使用 grep

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

egg
frog
horse

第二个是:

dog
cat
egg

输出应该是:1

你可以这样做:

#!/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 是文件 1 的路径,file2 是文件 2 的路径

试试这个

grep -f 文件 1 文件 2 | wc -l