How do I search multiple strings or words using the grep command? For example I'd like to search word1, word2, word3 and so on within /path/to/file. How do I force grep to search multiple words?
The grep command supports regular expression pattern. To search multiple words, use following syntax:
grep 'word1\|word2\|word3' /path/to/file
In this example, search warning, error, and critical words in a text log file called /var/log/messages, enter:
$ grep 'warning\|error\|critical' /var/log/messages
To just match words, add -w swith:
$ grep -w 'warning\|error\|critical' /var/log/messages
egrep command can skip the above syntax and use the following syntax:
$ egrep -w 'warning|error|critical' /var/log/messages
I recommend that you pass the -i (ignore case) and --color option as follows:
$ egrep -wi --color 'warning|error|critical' /var/log/messages
Sample outputs:
You should follow me on twitter here or grab rss feed to keep track of new changes.
This FAQ entry is 3 of 7 in the "Linux / UNIX grep Command Tutorial" series. Keep reading the rest of the series:





![HowTo: Use grep Command In Linux / UNIX [ Examples ]](http://s13.cyberciti.org/images/shared/rp/3/28.jpg)








{ 69 comments… read them below or add one }
you forget “\” so the command to search multiple string using grep as following
grep ‘warning\|error\|critical’ /var/log/messages
Thank you for this. I couldn’t figure out why it wasn’t working.
And why did Vivek not correct this yet?
Please.
This is the first result in google. Please make this correction.
The faq has been updated.
Appreciate all feedback.
It is good explain for grep
but i want find out one particular string from another string. shall i use grep.
Thanks for the article. Is there a way to supply the words in a file? So in your example I could create a simple file containing:
warning
error
critical
I have over 200 words that I need to search – which is tiresome to put onto the command line.
Thanks,
Use the -f FILE option to obtain patterns from FILE, one per line:
grep -f words.txt /var/log/messageswords.txt
Fantastic !! I had a scenario where in I had to pick up lines from a log file based on a word. Unfortunately I had 1000 such words and so it would have been difficult to find 1000 lines. But this solution really helped. Thanks.
Hi,
I have a requirement in which I would need to grep/find a line based on matching 3 different patterns. I know we can grep with -E multiple parameters seprated by pipe but this work Pipe (|) as OR condition. My requirement is I want to use AND condition. It should show line where it satisfy both the parameter.
e.g. line in a file as –
10-Aug-2010 Hello, this is a new example for unix.
I need to show this line only when my grep command matches all 3 words
this, new, unix How to right such grep command ?
grep -n “$search1″ . | grep -n “$search2″ | grep -n “$search3″
after the first grep only statements “search1″ will come. this is filtered by the next grep creating and AND condition.
this is a round about solution coming out the top of my mind. if i find a better solution i’ll post it.
this dint work for me:(
i used the cmd like grep -il SDI *.sql | grep -il Account *.sql | grep -il Customer *.sql
My requirement is as follows :
in the current folder, inside the files with extension .sql, i need to get the files hwich have SDI,Account and Costomer words in it.
-l, –files-with-matches
Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning
will stop on the first match.
did -l work for u??
How do you search for 2 strings on the same line?, but return the following line?
grep ‘matching\|\(matching again\)’
how do we search for two words and return it when both words are exists in the line. just like AND operator.
just pipe two greps grep ‘hello’ file.txt | grep ‘world’
these all are not working
try
grep -E -e ‘warning|critical|error’ /var/log/messages in Linux
/usr/xpg4/bin/grep -E -e ‘warning|critical|error’ /var/adm/messages
in solaris
How do I only return lines that have both words in the line
grep -E “rob|bob” returns lines even if only one string is present.
try
echo “hi rob, where is bob?” | grep -E -o “rob|bob”
it should return:
rob
bob
use
grep ‘rob’ | grep ‘bob’
Use awk.
$ echo “hi rob, where is bob” | awk '/rob/ && /bob/ {print $0}'Thanks, awk saved my day ;) as i understand it’s not just awk it’s regexp too?!
so thanks again, the day was saved by the power puff girs… o0O0ops… awk and regexp!
:)
I figured I should be more specific in my request.
I have the following line;
/usr/bin/dsmc archive -des=”DAILY” -archm=FS_DBARCH_DAILY “/brlog1/BIDW/redo/*”
I want a grep command to return the line number of this line
grep archive filename | grep -n redo doesn’t work because it returns the number of the line in the results from the first grep.
Thanks a lot.This saved my time. We have a delivery tomorrow.
@Nauman Ali,
To search for two words, and to return only if both words exist in a file, use this command
grep -Rl word1 *| xargs grep -l word2
Sasikala: THANK YOU!!!! That was very thoughtful of you to leave that for us, I needed it bad!!! :)
grep -Rl word1 *| xargs grep -l word2… how this work?
“grep -Rl word1 *| xargs grep -l word2… how this work?”
It just doesn’t work.
It will return the filename where exists at least one line where the two words are present. Which is no information at all.
@Robert Guest:
Attach the ‘-n’ parameter to the first grep, not to the second:
grep -n archive filename | grep redo
that way the second grep will get a line that contains the line number.
Just try this one:
grep word1 | grep word2
correction … :
grep word1 filename | grep word2
That won’t work if you need to check if word 1 and word2 are anywhere in the file, because the first grep returns the matching line, so the second grep would only match if both words were on the same line.
How do you search for the string below in a file using grep ?
DeptId = ’123459′
Any help is appreciated.
Hi,
I want to search multiple string using grep and want to display only matching string.
can any one pl guide me in this regards.
eg.
cat abc.txt |grep -e ‘ab’ -e ‘bc’ -e ‘cd’
If ‘bc’ is there in the file abc.txt then output should display only ‘bc’ rather than displaying the entire line.
Thanks & Regards,
Omprakash
Google for: grep multiple string and returning matching string
How to search multiple words in separate lines, inside a directory including sub-directory? Pls. give easy example.
I tried $grep -r “word1″ |grep -r “word2″ /Folder/subfolder/ > search.log
try
$grep -r “word1″ /Folder/subfolder/ | grep “word2″ > search.log
hi
I have to grep exact line in file
for eg : file name test.txt includes
ram
5) 1,2,3
sohan
5) 6,7,8
so i want to grep ram and 5 so the output shud be
ram
1,2,3
should not be
ram
1,2,3
6,7,8
First: grep is a single line utility. And because “5)” is on two lines, grep will find them, because it walks down the lines, and matches them to your rules, period.
Second: what grep finds, that ‘entire’ line will be displayed. So it is not possible to cut the “5)” from the beginning of the line, and display the rest.
If you want to find “ram” and after that the next “5)”, you need to use some utility that allows you to implement some logic. Like awk.
However if you can ensure that “5)…” will be after the found line – like “ram” – then you can use the ‘after context’ feature of grep, whic displayes the matching lines PLUS some line after that.
use this :
grep ‘ram|5)’ test.txt | head -2 | tr -d ’5)’
Hi,
I need to list the files which contains the 3 strings
<Tax
<Source
HEAD
These all 3 strings may be in different lines.
Thanks, Kumar
@Kumar and others
To search for multiple strings in a file try doing this :
For eg:
Let’s say I want to search for all those log4j.xml files which have the words CONSOLE and ASYNC in them .. then this is what I would do :
Cheers!
hi.
how to search a single line using grep command..
for example a file having 100 lines in that 100 line only one error line is there.
how do i retrive that single line using grep command.. i don’t know in which line in that error msg and like that error msg many of the lines in that file.. how do i find using ‘Grep’ command..
do u have any keyword to identify the error message??
for eg. if the keyword is ‘error:’
then
grep -rn “error:*”
its
grep -n “error:*” filename
since its one file -r is not necessary.
Thanks a lot ,it works fine
hi, i want to grep the lines which has Eg:”uat” string in the but not “#” string in the line.. can anyone help me out in this????
/usr/xpg4/bin/grep -E ‘error|critical’ sample.txt
this works for me..
Could you please help me regarding while connect putty Linux based logs like collect grep is working
tail -f test_log | grep ‘\” 50[0234]‘ – working
tail -f test_log | grep ‘\” 50[0234]‘ | grep “404″ – not working
Any one help me on this regard? how to collect 1.500 to 504 2.404 alone.
Great info site.
I have a need to search a file looking for dates and a string, For example:
(Dec 2 13:25:27 name local5:warn|warning vmdaemon[180412]: #415 Moved volume tape_1 #0055 (12345678) (abc123 from online to offline.). I need to search for a date and the “online to offline” string together. Also, with grep is it possible to do a date range in the search as oppose to a single date?
It is very good for the student and all type of reader.
I also wanted to find all files that contained two separate words. The words could be anywhere in the file.
One way that I found to do this is:
grep -l word1 `grep -l word2 *`
The backtick quotes execute what is inside the quotes and replace it with the results. -l returns file names only.
More commonly I would also include -ir for case-ignorant recursive search:
grep -irl word1 `grep -irl word2 *`
Here’s an interesting one that I have been wracking my brain on…
I have some fairly large data files I need to search for certain conditions. Easy enough because I know the string I want to search for. However, the problem arises when someone duplicates the string in the field that I am searching for which expands the data criteria past my grep. here’s example:
String = “000/C///” Field delimiter is pipe so I use: grep -i “|000/C///|” to match the condition. Now what I have found some people doing is using multiple strings in that field where only one exists. Is there a way to search for multiples of the same string without having to do a ton of greps in a row?
Trying to do things with the -A -B options –
To display every line mentioning an astrological element:
grep “earth|air|fire|water” astro.log
How do I accomplish this new result with grep – I cannot figure out how to use the options -A and -B more than once within the same command.
earth and the 2 lines following
air – only the line containing air
fire and the 6 lines following
water and the 3 lines preceding
This would result in a 15-line capture if the command works.
TRY…!!
cat ASHOK.txt|grep -E word1|word2
is working for me… :)
I am trying to pass the word/pattern from command line which I want to find in array but its not working. Say I want to find Jacob but its not returning anything.
#! usr/bin/perl use Fcntl; print "content-type: text/html \n\n"; print "Enter word : "; $word = ; @myNames = ('Jacob', 'Michael', 'Joshua', 'Matthew', 'Alexander', 'Andrew'); @grepNames = grep(/$word/, @myNames); foreach $Names(@grepNames) { print $Names; print "\n"; }For me it works only if grep is set to use extended regular expressions with the -E flag
grep -E One\|Two\|”Thirty five”
Versions
GNU bash, version 4.1.2(1)-release (x86_64-koji-linux-gnu)
GNU grep 2.6.3
How do I used grep to fiind a vaue with a \\ contained within. For example. If I do a “cat /var/tmp/file | grep “NT Server\\Test” Nothing is returned.
Try:
cat /var/tmp/file | egrep '[a-z]\\{2}[a-z]'I have a file “nynorsk-utf-8.txt” in UTF-8 that is supposed to be in Nynorsk, one of the two official dialects of Norwegian. But it contains some “contamination” of Bokmal, the other official dialect. To try to identify the sections of Bokmal, I want to find all lines in nynorsk-utf-8.txt that contain any one of a set of 13 short words that are exclusive to Bokmal:
ikke|jeg|fra|en|et|de|mye|hun|noen|se|selv|særlig|uke
I’m on OS X, using egrep (GNU grep) 2.5.1
I’ve tried the following:
egrep -in “\” nynorsk-utf_8.txt > out.txt
using \ to enforce word boundaries. Yet the output contains lines that do not contain any of these 13 words (as stand-alone words). I also tried
egrep -inw ‘ikke|jeg|fra|en|et|de|mye|hun|noen|se|selv|særlig|uke’ nynorsk-utf_8.txt > outw.txt
with the exact same results. Then I tried listing the 13 words, one per line, in a separate file ‘wordlist’, and launched
grep -inw -f wordlist nynorsk-utf_8.txt > outf.txt
again with the same results. E.g. each output file contains line 16
16:– Dei fleste meiner at rota til den nye terrorismen er å finne i ein tilnærma samfunnsstrukturell stillstand – ein tilstand som pregar både dei rike og fattige arabiske landa.
that does not contain any of the 13 words (as a stand-alone word).
Can anyone tell me what I’m doing wrong? or is there, perchance, a big in (e)grep?
Sorry–I’m new to this list, and my first example got corrupted. Let me try to mark it up to display correctly
i.e. I used \< and \> as explicit word boundaries. The results were equivalent to launching
Hi im facing a prob in matching multiple patterns.
CODE:
Variable=”abc* def*” # two patterns abc* and def* are stored in variable.
I should use variable to find the matching lines. How can i achieve it.
We use -f option if patterns are in a file. How to use when patterns are in a variable???
Hi All,
I have two text files
file one contains many words (single word per line) and file two contains many strings per line .
Now I want to Grep every line from file2 which contains file1 word.
Could any one help me out ?
This is like extracting from file2 matches from file1 !!
Very appreciated to the one who give solution here !
I want the code to Grep the row from file2 containing file1 strings !!!
Eg :
1.file2.txt
Happy Diwali
All is well
One day
2.file1.txt
Happy
One
Now I want to Grep 1st and 3rd line from the file2.txt by file1.txt
Appreciated answer !
!!!! Grep -f file1.txt file2.txt
Works guys , cheers ;))
THanks nixCraft ,, it’s very useful
Best answer would be to use the egrep command to get this worked:-
egrep ‘(str1|str2|str3)’
For example:-
bash-3.00$ cat triana
Man is the most beautiful creator of god.
Man is grate among all creators.
Awsome one…
Solution:-
bash-3.00$ egrep ‘(grate|god)’ triana
Man is the most beautiful creator of god.
Man is grate among all creators.
Hello,
I want to grep the pattern
‘
‘
but i think grep command is not reading newline. Please give me any solution if any one have.