如何从一个文件中的另一个文件中查找单词? [英] How to find words from one file in another file?

查看:20
本文介绍了如何从一个文件中的另一个文件中查找单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个文本文件中,我有 150 个字.我还有另一个文本文件,大约有 100,000 行.

In one text file, I have 150 words. I have another text file, which has about 100,000 lines.

如何检查属于第一个文件的每个单词是否在第二个文件中?

我想过使用grep,但是我不知道如何使用它来读取原文中的每个单词.

I thought about using grep, but I could not find out how to use it to read each of the words in the original text.

有没有办法使用 awk 做到这一点?或者其他解决方案?

Is there any way to do this using awk? Or another solution?

我尝试过这个 shell 脚本,但它几乎匹配每一行:

I tried with this shell script, but it matches almost every line:

#!/usr/bin/env sh
cat words.txt | while read line; do  
    if grep -F "$FILENAME" text.txt
    then
        echo "Se encontró $line"
    fi
done

我发现的另一种方法是:

Another way I found is:

fgrep -w -o -f "words.txt" "text.txt"

推荐答案

您可以使用 grep -f:

grep -Ff "first-file" "second-file"

OR else 匹配完整的单词:

OR else to match full words:

grep -w -Ff "first-file" "second-file"

更新:根据评论:

awk 'FNR==NR{a[$1]; next} ($1 in a){delete a[$1]; print $1}' file1 file2

这篇关于如何从一个文件中的另一个文件中查找单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆