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

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

问题描述

在一个文本文件中,我有150个单词。我有另一个文本文件,它有大约100,000行。



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



我想过使用 grep ,但我找不到如何使用它读取原文中的每个单词。



有什么方法可以使用 awk 来完成这个任务吗?或另一种解决方案?



我尝试了这个shell脚本,但几乎可以匹配所有行:

 #!/ usr / bin / env sh 
cat words.txt |而读线;做
if grep -F$ FILENAMEtext.txt
然后
echoSeencontró$ line
fi
完成

我找到的另一种方法是:

pre $ fgrep -w -o -fwords.txttext.txt


解决方案您可以使用 fgrep -f

  fgrep -ffirst-filesecond-file

或者匹配全部单词:

  fgrep -w -ffirst-filesecond-file


$ b

更新:根据评论:

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


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

How can I check for each of the words belonging to the first file whether it is in the second or not?

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.

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

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"

解决方案

You can use fgrep -f:

fgrep -f "first-file" "second-file"

OR else to match full words:

fgrep -w -f "first-file" "second-file"

UPDATE: As per the comments:

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

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

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