Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I've got a bunch on strings which I need to find in a couple of files, for example:

string1
string2
stringn

file1.txt
file2.txt
filen.txt

Is there an (easy) way to do that in bash? I need to know, if a string was found, in which file is it.

share|improve this question

migrated from stackoverflow.com Apr 28 '11 at 16:25

This question came from our site for professional and enthusiast programmers.

6 Answers 6

up vote 5 down vote accepted

Simple grep command with -e option:

 grep -e "string1" -e "string2" -e "stringn" file*.txt

Or you can put all the search strings in a file called search.txt like this:

string1
string2
string3
...
...
stringN

and then run grep like this with -f option:

grep -f search.txt file*.txt
share|improve this answer
    
you can use stdin as file echo -e "string1\nstring2\nstringn" | grep -f - search.txt file*.txt –  jcubic Apr 28 '11 at 15:24
    
Thank you so much, it worked flawlessly. –  AbraKdabra Apr 28 '11 at 15:24
    
My pleasure. If you think it was useful then pls mark the answer as accepted also :) –  anubhava Apr 28 '11 at 16:22

Use grep to search for all the strings in one pass:

grep -E -H 'string1|string2|stringn' file1.txt file2.txt filen.txt

The -E lets you use the pipe character(|) without escaping it. The -H prints the filename for each match. The regular expression uses pipes to separate each string, so that grep will try to match each one in order.

share|improve this answer

There is a variant of grep that supports this feature for large sets of strings, try

fileWithListOfSearchTargets=myFileOfSearchTargets.txt

fgrep -f ${fileWithListOfSearchTargets} file1 file2 ... filen

(The variable and filenames are meant to be self-documenting, you can use any name you like)

You have to enter all your search strings into the file.

No leading or trailing spaces unless you expect those to match in your filelist. There is a limit to the size that most fgreps can process. Don't try to cram 10K lines into one file.

I hope this helps.

P.S. as you appear to be a new user, if you get an answer that helps you please remember to mark it as accepted, and/or give it a + (or -) as a useful answer.

share|improve this answer

The best way is to use grep:

grep -H 'string to search' file1.txt file2.txt filen.txt

will search the specified files for a string, and print out the matching lines along with the filename where the match was found.

share|improve this answer
    
And if you need to do this for an arbitrary number of strings, enclose it in a for loop. For example: for searchString in "string1 string2 stringn"; do grep -H $searchString file1.txt file2.txt; done However, this assumes that searchString is an easy word -- if it has symbols, spaces, you need more escaping. –  Mike R Apr 28 '11 at 15:13

Unix find command:

find . -exec grep "i want to find this string" '{}' \; -print

will search from current dir and down.

This works too:

egrep -r 'arbitrary string' *
share|improve this answer
    
The use of find here does not add anything that grep cannot do by itself. Of course find COULD be used to a purpose to find a useful set of files to send to grep, but in this case grep would be able to do the same with with the -R flag and save a lot of mess. –  Caleb May 7 '11 at 11:15
egrep "(string1|string2|string3)" file{1..3}.txt
share|improve this answer
    
This adds nothing useful not covered in other answers. If you want to show alternate syntax consider commenting on one of the other answers instead. –  Caleb May 7 '11 at 11:14
    
I introduced the syntax of curly braces, to address multiple files. We had the discussion already on meta. –  user unknown May 7 '11 at 12:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.