bash 从文本文件中提取多个字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48120921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:44:13  来源:igfitidea点击:

Grep multiple strings from text file

bashawkgrep

提问by user3255841

Okay so I have a textfile containing multiple strings, example of this -

好的,所以我有一个包含多个字符串的文本文件,例如 -

Hello123
Halo123
Gracias
Thank you
...

I want grep to use these strings to find lines with matching strings/keywords from other files within a directory

我希望 grep 使用这些字符串从目录中的其他文件中查找具有匹配字符串/关键字的行

example of text files being grepped -

文本文件被 grep 的示例 -

123-example-Halo123
321-example-Gracias-com-no
321-example-match

so in this instance the output should be

所以在这种情况下,输出应该是

123-example-Halo123
321-example-Gracias-com-no

回答by Cyrus

With GNU grep:

使用 GNU grep:

grep -f file1 file2

-f FILE: Obtain patterns from FILE, one per line.

-f FILE: 从 FILE 获取模式,每行一个。

Output:

输出:

123-example-Halo123
321-example-Gracias-com-no

回答by Tom Drake

You should probably look at the manpage for grep to get a better understanding of what options are supported by the grep utility. However, there a number of ways to achieve what you're trying to accomplish. Here's one approach:

您可能应该查看 grep 的联机帮助页,以更好地了解 grep 实用程序支持哪些选项。但是,有多种方法可以实现您的目标。这是一种方法:

grep -e "Hello123" -e "Halo123" -e "Gracias" -e "Thank you" list_of_files_to_search

However, since your search strings are already in a separate file, you would probably want to use this approach:

但是,由于您的搜索字符串已经在一个单独的文件中,您可能希望使用这种方法:

grep -f patternFile list_of_files_to_search

回答by Or B

I can think of two possible solutions for your question:

对于您的问题,我可以想到两种可能的解决方案:

  1. Use multiple regular expressions - a regular expression for each word you want to find, for example:

    grep -e Hello123 -e Halo123 file_to_search.txt
    
  2. Use a single regular expression with an "or" operator. Using Perl regular expressions, it will look like the following:

    grep -P "Hello123|Halo123" file_to_search.txt
    
  1. 使用多个正则表达式 - 您要查找的每个单词的正则表达式,例如:

    grep -e Hello123 -e Halo123 file_to_search.txt
    
  2. 使用带有“或”运算符的单个正则表达式。使用 Perl 正则表达式,它将如下所示:

    grep -P "Hello123|Halo123" file_to_search.txt
    

EDIT:As you mentioned in your comment, you want to use a list of words to find from a file and search in a full directory.

编辑:正如您在评论中提到的,您想使用单词列表从文件中查找并在完整目录中搜索。

You can manipulate the words-to-find file to look like -eflags concatenation:

您可以操作 words-to-find 文件,使其看起来像-e标志连接:

cat words_to_find.txt | sed 's/^/-e "/;s/$/"/' | tr '\n' ' '

This will return something like -e "Hello123" -e "Halo123" -e "Gracias" -e" Thank you", which you can then pass to grepusing xargs:

这将返回类似的内容-e "Hello123" -e "Halo123" -e "Gracias" -e" Thank you",然后您可以将其传递给grepusing xargs

cat words_to_find.txt | sed 's/^/-e "/;s/$/"/' | tr '\n' ' ' | dir_to_search/*

As you can see, the last command also searches in all of the files in the directory.

如您所见,最后一个命令还搜索目录中的所有文件。

SECOND EDIT: as PesaThe mentioned, the following command would do this in a much more simple and elegant way:

第二次编辑:正如 PesaThe 所提到的,以下命令将以更简单和优雅的方式执行此操作:

grep -f words_to_find.txt dir_to_search/*

grep -f words_to_find.txt dir_to_search/*