Stefano
Stefano

Reputation: 4031

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

Upvotes: 2

Views: 8629

Answers (5)

jaypal singh
jaypal singh

Reputation: 77075

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

Upvotes: 7

jflaflamme
jflaflamme

Reputation: 1797

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

Upvotes: 1

Fredrik Pihl
Fredrik Pihl

Reputation: 45634

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}

Upvotes: 4

Hai Vu
Hai Vu

Reputation: 40688

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

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

Upvotes: 1

Marc B
Marc B

Reputation: 360572

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.

Upvotes: 3

Related Questions