Search Multiple Words / String Pattern Using grep Command on Bash shell

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? How can I grep for multiple patterns on Linux, OS X, FreeBSD, or Unix-like system? What is the command to search multiple words in Linux?

The grep command supports regular expression pattern. We can easily grep two words or string using the grep/egrep command on Linux and Unix-like systems. To search multiple patterns, use the following syntax:

ADVERTISEMENTS

How do I grep for multiple patterns?

The syntax is:

  1. Use single quotes in the pattern: grep 'pattern*' file1 file2
  2. Next use extended regular expressions: egrep 'pattern1|pattern2' *.py
  3. Finally, try on older Unix shells/oses: grep -e pattern1 -e pattern2 *.pl
  4. Another option to grep two strings: grep 'word1\|word2' input

Grep searching two words in a line

Here are all other possibilities for grep and egrep command:
grep 'word1\|word2\|word3' /path/to/file
### Search all text files ###
grep 'word*' *.txt
### Search all python files for 'wordA' or 'wordB' ###
grep 'wordA*'\''wordB' *.py
grep -E 'word1|word2' *.doc
grep -e string1 -e string2 *.pl
egrep "word1|word2" *.c
### Show all the lines that do not match given pattern/words/strings ###
grep -v 'bar\|foo' /dir1/dir2/file1
egrep -v 'pattern1|pattern2' /path/to/file

Examples – How to Grep for Multiple Strings, Patterns or Words

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 the -w option:
$ grep -w 'warning\|error\|critical' /var/log/messages
Use the egrep command and you can skip the above syntax to search three words:
$ egrep -w 'warning|error|critical' /var/log/messages

Grep multiple patterns

Sometimes we need to grep multiple patterns with special character such as ‘-‘ or ‘--‘. For example, search for pattern starting with ‘--ca‘ or ‘--no‘ words. So if you try the following you will get an error on screen such as “grep: unrecognized option : --ca|--no“. Let us try an example:
$ acme.sh --help | egrep '--ca|--no'
In order to tell grep not to treat ‘--‘ as command line option prefix pattern as follows:
$ acme.sh --help | egrep -- '--ca|--no'
$ virt-sysprep --help | egrep -- '--(truncate|run)'

All other grep or egrep command option must appear before the final --. For instance pass the -w and --color as follows:
acme.sh --help | egrep --color -w -- '--ca|--no'
How to Grep for Multiple Patterns on Linux and Unix

How to find multiple strings in files?

Let us try a few more examples with additional options passed to the grep/egrep:
$ grep -e 'warning\|error\|critical' /var/log/messages
I recommend that you pass the -i (ignore case) and --color option as follows too:
$ egrep -wi --color 'warning|error|critical' /var/log/messages
Sample outputs:

Fig.01: Linux / Unix egrep Command Search Multiple Words Demo Output

Fig.01: Linux / Unix egrep Command Search Multiple Words Demo Output

When using grep command, pipe (|) meta-character must be escaped with a backslash, and there are no such requirements for egrep.
To search all *.conf files under /etc/, enter:
# egrep -wi --color 'foo|bar' /etc/*.conf
To search recursively (including sub-directories) listed, run:
# egrep -Rwi --color 'foo|bar' /etc/
Where options are as follows:

  • -R : Recursive search
  • -w : Match only words
  • -i : Ignore case distinctions. In other words match FOO, foo, Foo and so on.
  • --color : Surround the matched in color on the terminal. For example, display matched strings in colors.
  • -v : Invert the sense of matching, to select non-matching line. In other words, search and display all the lines, that do not match our strings or words

grep multiple strings using awk

Say if you are already using the awk command or sed command command, then there is no need to pipe out to grep and feed data from grep. We can process and gather multiple strings using awk or sed as follows to save CPU cycle:

awk command syntax

$ awk '/error|critical/failed/' /var/log/httpd/error_log
## case instive search with *gnu/awk* ##
$ awk 'BEGIN{IGNORECASE=1} /error|critical/failed/' /var/log/messages
$ awk '/word1.*word2/' input
$ awk '/myPattern1/ && /myPattern2/' /path/to/file
## awk not matching i.e. show all line except HTTP/2.0 logs ##

$ awk '!/HTTP\/2.0/' /var/log/nginx/cyberciti.bizerror_log

sed command syntax

$ sed -e '/error/b' -e '/critcial/b' -e d /var/log/apache/nixcraft.com_error_log
$ sed '/stringOne/!d; /stringTwo/!d' ~/backups/conf.txt
## sed negative (NOT) matching. For example, show all hosts except cbz01-www ##
$ sed -n '/cbz01-www/!p' /etc/hosts

As you can observe, grep syntax is easy to read and implement. However, I provided awk and sed syntax for your shell scripting needs too.

Conclusion

You learned how to use grep command to locate multiple words or strings in a single go. For more information see our all other grep related tutorials or read grep man page by typing the following man command. Alternatively, you can read it online here:

This entry is 3 of 7 in the Linux / UNIX grep Command Tutorial series. Keep reading the rest of the series:
  1. How To Use grep Command In Linux / UNIX
  2. Regular Expressions In grep
  3. Search Multiple Words / String Pattern Using grep Command
  4. Grep Count Lines If a String / Word Matches
  5. Grep From Files and Display the File Name
  6. How To Find Files by Content Under UNIX
  7. grep command: View Only Configuration File Directives

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
104 comments… add one
  • Ayman Elkazzaz Jun 19, 2008 @ 23:20

    you forget “\” so the command to search multiple string using grep as following
    grep ‘warning\|error\|critical’ /var/log/messages

    • Matt Jan 18, 2012 @ 10:44

      Thank you for this. I couldn’t figure out why it wasn’t working.

    • David Mar 23, 2012 @ 13:36

      And why did Vivek not correct this yet?

      Please.

    • Aswin Aug 10, 2012 @ 10:57

      This is the first result in google. Please make this correction.

      • 🐧 nixCraft Aug 10, 2012 @ 11:15

        The faq has been updated.

        Appreciate all feedback.

    • khali Feb 28, 2014 @ 14:59

      thanks. it helped

  • S. Mohana Jul 16, 2008 @ 4:39

    It is good explain for grep

    but i want find out one particular string from another string. shall i use grep.

  • John Sep 17, 2008 @ 14:37

    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,

  • 🐧 nixCraft Sep 17, 2008 @ 16:24

    Use the -f FILE option to obtain patterns from FILE, one per line:
    grep -f words.txt /var/log/messages
    words.txt

    word1
    word2
    wordN
    • Hemal Jul 7, 2011 @ 12:08

      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.

      • Mats Aug 30, 2011 @ 12:37

        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 ?

        • pop richards Aug 30, 2011 @ 12:56

          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.

          • bharath May 7, 2012 @ 10:48

            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.

            • pop richards May 7, 2012 @ 13:09

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

  • pfaure Feb 4, 2009 @ 2:38

    How do you search for 2 strings on the same line?, but return the following line?

    • bambam Aug 16, 2012 @ 2:07

      grep ‘matching\|\(matching again\)’

  • Nauman Ali Mar 5, 2009 @ 10:19

    how do we search for two words and return it when both words are exists in the line. just like AND operator.

  • maginot Sep 9, 2009 @ 13:55

    just pipe two greps grep ‘hello’ file.txt | grep ‘world’

  • relesh Sep 23, 2009 @ 8:41

    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

    • Michael Loutris Feb 14, 2015 @ 0:38

      The problem is that you need to ‘escape’ the or operator ‘|’ as shown above AND use the ‘i’ flag to be case insensitive:

      grep -i 'warning\|critical\|error'  /var/adm/messages
  • relesh Sep 23, 2009 @ 8:42

    in solaris

    • sam Aug 22, 2013 @ 11:46

      It doesn’t work in Solaris. Do anyone have any commands to find multiple string (say string1 and string2) in a file and retunrs the line as well?

      Thanks in advance.

  • Robert Guest Sep 25, 2009 @ 17:39

    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.

    • Dr.Death Oct 13, 2010 @ 10:57

      try
      echo “hi rob, where is bob?” | grep -E -o “rob|bob”

      it should return:
      rob
      bob

      • anoop Jul 27, 2011 @ 9:55

        use
        grep ‘rob’ | grep ‘bob’

    • bambam Aug 23, 2012 @ 17:52

      Use awk.

      $ echo “hi rob, where is bob” | awk '/rob/ && /bob/ {print $0}'
      • lpic.lt Jan 9, 2013 @ 7:08

        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!

        :)

  • Robert Guest Sep 25, 2009 @ 17:50

    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.

  • Sarat Dec 11, 2009 @ 13:56

    Thanks a lot.This saved my time. We have a delivery tomorrow.

  • Sasikala Jan 15, 2010 @ 7:18

    @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

    • Subhash Jun 10, 2014 @ 15:35

      Thanks….that helped

  • johnny Jan 15, 2010 @ 18:03

    Sasikala: THANK YOU!!!! That was very thoughtful of you to leave that for us, I needed it bad!!! :)

  • Nagendra Prasath Jan 30, 2010 @ 13:03

    grep -Rl word1 *| xargs grep -l word2… how this work?

  • karatedog Feb 27, 2010 @ 1:50

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

  • karatedog Feb 27, 2010 @ 1:58

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

  • GUSTAVO COST Jun 2, 2010 @ 2:51

    Just try this one:

    grep word1 | grep word2

  • GUSTAVO COSTA Jun 2, 2010 @ 2:52

    correction … :
    grep word1 filename | grep word2

    • Anthony Jun 20, 2011 @ 22:32

      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.

  • MUAnis Jul 13, 2010 @ 21:53

    How do you search for the string below in a file using grep ?

    DeptId = ‘123459’

    Any help is appreciated.

    • Michael Loutris Feb 14, 2015 @ 0:43

      Wrap the entire search string in double quotes:

      grep  "DeptId = '123459'"  *
  • Omprakash Sep 15, 2010 @ 9:05

    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

  • Ritika Dec 16, 2010 @ 8:31

    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

  • richardspop Mar 8, 2011 @ 10:07

    try
    $grep -r “word1″ /Folder/subfolder/ | grep “word2″ > search.log

  • vaibhav Mar 20, 2011 @ 9:28

    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

    • karatedog Mar 20, 2011 @ 20:03

      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.

      • tsg Apr 19, 2012 @ 10:56

        use this :

        grep ‘ram|5)’ test.txt | head -2 | tr -d ‘5)’

  • kumar Apr 25, 2011 @ 9:10

    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

    • ajorpheus May 11, 2011 @ 16:25

      @Kumar and others

      To search for multiple strings in a file try doing this :

      grep -il "String1" "PATH-OF-FILE" | xargs -I % grep -H "String2" % | xargs -I % grep -H "String3" %
      

      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 :

      find "/cygdrive/e/MyDocs/Downloads/work/OATS Domain related/" -iname "log4j*.xml" | xargs -I % grep -ilr "CONSOLE" "%" | xargs -I % grep -H "ASYNC" %
      

      Cheers!

  • nithya May 17, 2011 @ 10:09

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

    • richardspop May 17, 2011 @ 12:09

      do u have any keyword to identify the error message??
      for eg. if the keyword is ‘error:’

      then

      grep -rn “error:*”

      • richardspop May 17, 2011 @ 12:11

        its
        grep -n “error:*” filename

        since its one file -r is not necessary.

  • vinoth Jul 14, 2011 @ 5:08

    echo year, month, Quarter,productTOT, RegionTOT,Per_type,Data_type,Bucket_noTOT,Curr_dateTOT

    from this output, i want only the string that has “TOT”

    so the output,

    productTOT, RegionTOT,Bucket_noTOT,Curr_dateTOT

    could anybody please help me???

  • Vivek Aug 3, 2011 @ 7:05

    Thanks a lot ,it works fine

  • Saravanan Aug 23, 2011 @ 4:43

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

  • Jass Sep 26, 2011 @ 20:17

    /usr/xpg4/bin/grep -E ‘error|critical’ sample.txt

    this works for me..

  • Abdulla Oct 19, 2011 @ 19:22

    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.

    • JV Jan 14, 2016 @ 17:06

      tail -f test_log | grep –line-buffered “blah” | grep –line-buffered “bluh”

  • Ramon Dec 8, 2011 @ 21:10

    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?

  • Om Prakash Feb 24, 2012 @ 10:23

    It is very good for the student and all type of reader.

  • Ben Mar 21, 2012 @ 0:51

    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 *`

  • Javier Mar 30, 2012 @ 23:06

    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?

  • jeff halper Jun 7, 2012 @ 14:35

    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.

  • ashok Jul 23, 2012 @ 22:59

    TRY…!!

    cat ASHOK.txt|grep -E word1|word2

    is working for me… :)

  • sam Jul 31, 2012 @ 22:18

    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.

    16472374815f9bfb9616ae0_000000

  • A1an Aug 2, 2012 @ 12:08

    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

  • Tailgate Aug 3, 2012 @ 15:19

    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.

    • bambam Aug 23, 2012 @ 17:46

      Try:

      cat /var/tmp/file | egrep  '[a-z]\\{2}[a-z]'
  • Ken Beesley Oct 26, 2012 @ 18:03

    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?

  • Kenneth Beesley Oct 29, 2012 @ 16:53

    Sorry–I’m new to this list, and my first example got corrupted. Let me try to mark it up to display correctly

    egrep -in “\<(ikke|jeg|fra|en|et|de|mye|hun|noen|se|selv|særlig|uke)\>” nynorsk-utf_8.txt > out.txt
    

    i.e. I used \< and \> as explicit word boundaries. The results were equivalent to launching

    egrep -inw “ikke|jeg|fra|en|et|de|mye|hun|noen|se|selv|særlig|uke” nynorsk-utf_8.txt > out.txt
    
  • Pradeep Oct 31, 2012 @ 7:01

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

  • Akbar Nov 10, 2012 @ 15:21

    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 !

  • Akbar Nov 10, 2012 @ 15:52

    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 !

  • Akbar Nov 10, 2012 @ 16:24

    !!!! Grep -f file1.txt file2.txt

    Works guys , cheers ;))

  • Pramod Dec 20, 2012 @ 22:21

    THanks nixCraft ,, it’s very useful

  • Vinay Jan 31, 2013 @ 14:25

    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.

  • Shilp Feb 14, 2013 @ 10:34

    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.

  • sony Mar 29, 2013 @ 8:31

    FILENAME=’trade.tmp’

    cat $FILENAME | while read line
    do
    #echo “$line”
    Keyword=`cut -d “)” -f 2 $line| cut -d “(” -f 2`
    Keyword_Count=`grep $KewKeyword $FILENAME`
    echo “$Keyword_Count,$Keyword” > trade1.log

    done

    HI ,

    In the above script i want to get the keyword and once i get the keyword I need to get the count of keyword.So i tried with the above script .it is showing the count first keyword only.Can anyone suggest me to resolve the problem.

    Thanks In advance

  • abcd Jun 21, 2013 @ 9:21

    how do i search for a pattern /someword*/
    here someword can be A-Z or a-z or 0-9
    Any reply would be appreciated.

    • Pradeep Jun 21, 2013 @ 10:02

      grep “someword\*” filename
      This will do your work!!

  • abcd Jun 21, 2013 @ 10:39

    Thanks for ur reply but I think i Did not tell my problem properly.

    My qustion is :-

    I need to search a pattern /someword*/ in a property file

    here someword can be any thing eg.A-Z or a-z or 0-9
    eg:-
    /goingTo/FollowingPattern*/=Yes
    /WhatASite/FloodsIn*/=No
    /dummyData/FinallyHere*/=QT

    here I have to search for patterns
    /FollowingPattern*/
    /FloodsIn*/
    /FinallyHere*/

    Hence someword is variable in property file.

    Any help is appreciated

    • tigerwhat Jan 22, 2014 @ 13:34

      Don’t this work?

      $echo "/goingTo/FollowingPattern*/=Yes" | grep "/[a-zA-Z]*/F[a-zA-Z]*\*/[A-Z]" 
      /goingTo/FollowingPattern*/=Yes
      
    • tigerwhat Jan 22, 2014 @ 13:41

      You said numbers too, so just put “[0-9]”

  • Huda Sep 4, 2013 @ 15:07

    What really works is
    grep -n “s1” *.* |grep -n “s2”

  • mouli Jan 22, 2014 @ 8:29

    how to get separate out files for multiple patterns [stings]

    • tigerwhat Jan 22, 2014 @ 13:25

      Iterate over for each.

  • rahul Jun 13, 2014 @ 20:17

    I need to calculate the received packets from .tr file.
    Problem is that one string is necessary for me but some unnecessary events are also counted that are not needed.

    So I want a solution.

    line1: r 0.500000000 _1_ RTR — 0 cbr 210 [0 0 0 0] ——- [1:0 5:0 32 0] [0] 0 0
    line 2: r 0.501408175 _3_ RTR — 0 AODV 48 [0 ffffffff 1 800] ——- [1:255 -1:255 30 0] [0x2 1 1 [5 0] [1 4]] (REQUEST)

    I want only line 1 but as I am searching for ‘^r’ only so both files are counting. Pls help me how can I search the line where 2 patterns are needed?

  • Raghuvir Singh Jul 9, 2014 @ 7:47

    I want to use grep command for multiple patterns in a file and print a number of lines after each match into a new file. Say in the command
    $grep ‘Well\|MultiLayer\|Prediction’ PredResult.prn >List.txt

    – after ‘Well’ match I want to print 100 lines starting with pattern ’01/’,
    – after ‘MultiLayer’ match I do not want to print any line,
    – after ‘Prediction’ match I want to print 100 lines starting with pattern ’01/’.

    Please suggest a workaround.

  • Gaurav Khurana Nov 3, 2014 @ 14:33

    egrep “pattern1|pattern2” filename
    egrep -e “pattern1|pattern2” filename

    The above(both) syntax works in Sun OS for searching multiple patterns in file

  • Pete Townsend Jan 14, 2015 @ 21:49

    How can I search with a date string with a space?
    Like I was to search for ‘Jan 14’ and Username

  • Thomas Lee Jul 10, 2015 @ 6:25

    Thanks for the super useful info.

  • Vijeth P O Aug 25, 2015 @ 6:28

    Use grep -E “pattern1|pattern2”

  • CeeDEE Sep 29, 2015 @ 19:31

    Maybe stupid question but I can’t understand WHY this one works at all:

    $ grep 'warning\|error\|critical' /var/log/messages 

    Shouldn’t the \ mean it will escape the | and treat it as a string value of ‘|’? It works and treats | as the regular expression OR (alternation) function, yes, but it’s driving me crazy. I am thinking I should just egrep everything if I plan to use regular expressions because that usage makes no sense to me and the egrep usage does.

  • Nan Xiao Dec 29, 2015 @ 4:05

    From this post:

    grep -E 'warning|error|critical' /var/log/messages
    grep -e warning -e error -e critical /var/log/messages

    should both OK!

  • Dhanaekaran Dec 29, 2015 @ 8:49

    Hi Guys,

    How to do that invert selection [grep -v] multiple string, without pipe. ?

    • 🐧 Vivek Gite Dec 29, 2015 @ 9:50

      Use it as follows:

      egrep -v 'word1|word2' file

  • Nagaraj Mar 4, 2016 @ 18:04

    Hi,
    Need help!
    I need to grep “timeout” with sourrending 2 lines. (grep -C 2 “timeout” filename) is not working since using HP-UX.

    Please let me know if there are any other commands for HP-UX.

    Thanks,
    Nagaraj

  • Manoj Apr 20, 2016 @ 20:09

    Is there a way where i can get individual count using multiple strings..
    say i want to get the individual count of each word using
    grep -i warning|error|critical’ /var/log/messages

  • DodoGTA GT Jun 30, 2016 @ 17:23

    Thanks for this tutorial 👍

  • srikanth Jul 18, 2016 @ 16:22

    Thanks bro. it is very useful.

  • O Aug 29, 2016 @ 8:22

    ps -ef | grep ‘8462|3705|1847|1739|1671|1650|1146|1108|1082’
    Will help to grep many strings in one command

  • Ahmed Sep 19, 2016 @ 8:57

    how can i make grep on statement but it is contain more than line

  • iqra Nov 1, 2016 @ 7:31

    write a unix command to List all files that have tab somewhere in their name?

  • iqra Nov 1, 2016 @ 7:32

    Enter the command grep if bashrc. This will find every line in the file that contains an if followed by a blank space. How many lines did the statement find? Were any of the lines not actually if statements? (comments start with a #). You can obtain just the number of matches using grep c. Repeat the command using c to make sure you counted correctly!

  • harsha Jan 24, 2017 @ 8:52

    Hi ,

    The below script is not working
    $ grep -e 'warning|error|critical' /var/log/messages

    so I tried to use ‘\|’ instead of ‘|’ alone
    $ grep -e 'warning\|error\|critical' /var/log/messages

    Then I got desired output… so does the above script varies based on OS ?? I am using
    Linux 3.0.101-0.47.93-default

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.