linux在一个文件中搜索多个单词

发布于 2024-12-07 04:03:01 字数 481 浏览 1 评论 0原文

我有一个包含一组文本文件的文件夹。

-Folder
--- file 1
--- file 2
--- file 3
--- file 4

我有一组单词,我想检查它是否在里面。 {word1, username, blah blahblah}

有没有办法通过单个命令来发现其中哪个文件包含我列表中的所有单词?

我看到可以使用一些和 grep 但我认为它们在一行上工作,而在我的情况下,wors总是在不同的行上。

字数是静态的。总是 3 或 4,所以如果需要,我可以在命令中对它们进行硬编码。

编辑: 他们在 AND 中。如果文件内没有全部内容,则不接受该文件! 我想避免这样做 egrep -l 'word1' .| xargs egrep -l 'word2'

有没有更好的解决方案来调用 grep 一次?

干杯, 斯特

I have a folder containing a set of text files.

-Folder
--- file 1
--- file 2
--- file 3
--- file 4

I have a set of word that i want to check if are inside. {word1, username, blah blahblah}

Is there a way on a single command to discover which of these file contains all the word within my list?

I saw it's possible to use some and with grep but i think they work on a single line while in my case the wors are always on different lines.

the number of word is static. are always 3 or 4 so if needed i can hard code them in the command.

EDIT:
They are in AND. a file is not accepted if does not have ALL of them inside!
i would like to avoid doing
egrep -l 'word1' .| xargs egrep -l 'word2'

Is there a better solution to call grep just once?

Cheers,
Ste

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

丘比特射中我 2024-12-14 04:03:01

这对你有用吗?

grep -IRE 'word1|username|blah blahblah' /path/to/files/ | 
sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P' | 
awk -F: '$1!=p{if(b"" && c > 2)print b; p=$1;c=0;b=s=""}{b=b s $0;s=RS;c++}END
{if(b"" && c > 2)print b}' | awk -F: '{print $1}' | sort -u

第一部分(grep)将列出所有具有匹配模式的文件名。第二部分(sed)将从第一个输出中去除重复项,仅给出不同的行。第三部分将仅显示出现多次的文件,第四部分将删除您匹配的模式,最后一部分将仅向您提供文件名我的朋友。

我现在头很痛...

Does this work for you?

grep -IRE 'word1|username|blah blahblah' /path/to/files/ | 
sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P' | 
awk -F: '$1!=p{if(b"" && c > 2)print b; p=$1;c=0;b=s=""}{b=b s $0;s=RS;c++}END
{if(b"" && c > 2)print b}' | awk -F: '{print $1}' | sort -u

The first part (grep) will list all the file names with matching pattern. The second part (sed) will strip the duplicates from the first output giving only distinct rows. The third part will only show the file which occurs more than once and the forth one will strip your matched pattern and the last one will only serve you the file name my friend.

my head hurts now ...

凉城 2024-12-14 04:03:01

使用:

grep -f words.txt input

示例:

$ cat words
word1
username
blah blahbla

a
word1
username blah blahblah
b
username blah blahblah
c
word1
d
word1, username, blah blahblah}

$ grep -f words.txt *
a:word1
a:username blah blahblah
b:username blah blahblah
c:word1
d:word1, username, blah blahblah}

use:

grep -f words.txt input

Example:

$ cat words
word1
username
blah blahbla

a
word1
username blah blahblah
b
username blah blahblah
c
word1
d
word1, username, blah blahblah}

$ grep -f words.txt *
a:word1
a:username blah blahblah
b:username blah blahblah
c:word1
d:word1, username, blah blahblah}
天暗了我发光 2024-12-14 04:03:01

使用 grep:

grep -E '(word1|username|blah blahblah)' Folder/*

-E 标志将 grep 置于正则表达式的“扩展”模式。默认情况下,这将显示文件名和匹配的文本。如果您只需要文件名,请将 -l 添加到选项中。

Use grep:

grep -E '(word1|username|blah blahblah)' Folder/*

the -E flag puts grep into 'extended' mode for regular expressions. This will by default show the filename AND the matching text. If you want just the filename, add -l to the options.

旧时浪漫 2024-12-14 04:03:01

另一种解决方案最适合一小部分单词:

grep -e word1 -e username -e "blah blahblah" Folder/*

Another solution, which works best for a small set of words:

grep -e word1 -e username -e "blah blahblah" Folder/*
空气里的味道 2024-12-14 04:03:01

如果您想 grep 到目录树中,

egrep -E '{word1|username|blah blahblah)' `find . -type f -print` 

我建议您在搜索有关 *nix 系统的答案时也使用术语目录而不是文件夹:-)

The following if you want to grep into a directory tree

egrep -E '{word1|username|blah blahblah)' `find . -type f -print` 

I suggest you also to use the term directory instead of folder when you are searching for answers about *nix systems :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文