简体   繁体   中英

linux search multiple word in a files

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

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 ...

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}

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.

另一个解决方案,最适合一小组单词:

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

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 :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM