如何使用单词列表进行 grep [英] How to grep with a list of words

查看:15
本文介绍了如何使用单词列表进行 grep的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件 A,里面有 100 个单词,用新行分隔.我想搜索文件B,看看文件A中是否有任何单词出现在其中.

I have a file A with 100 words in it separated by new lines. I would like to search file B to see if ANY of the words in file A occur in it.

我尝试了以下方法但对我不起作用:

I tried the following but does not work to me:

grep -F A B

推荐答案

你需要使用选项-f:

$ grep -f A B

选项 -F 执行固定字符串搜索,而 -f 用于指定模式文件.如果文件只包含固定字符串而不包含正则表达式,您可能需要两者.

The option -F does a fixed string search where as -f is for specifying a file of patterns. You may want both if the file only contains fixed strings and not regexps.

$ grep -Ff A B

您可能还需要 -w 选项来仅匹配整个单词:

You may also want the -w option for matching whole words only:

$ grep -wFf A B

阅读 man grep 以了解所有可能的参数及其作用的描述.

Read man grep for a description of all the possible arguments and what they do.

这篇关于如何使用单词列表进行 grep的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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