How do I grep for multiple patterns with pattern having a pipe character?












541















I want to find all lines in several files that match one of two patterns. I tried to find the patterns I'm looking for by typing



grep (foo|bar) *.txt


but the shell interprets the | as a pipe and complains when bar isn't an executable.



How can I grep for multiple patterns in the same set of files?










share|improve this question

























  • possible duplicate of Grep: how to add an “OR” condition?

    – phuclv
    Nov 14 '17 at 2:26
















541















I want to find all lines in several files that match one of two patterns. I tried to find the patterns I'm looking for by typing



grep (foo|bar) *.txt


but the shell interprets the | as a pipe and complains when bar isn't an executable.



How can I grep for multiple patterns in the same set of files?










share|improve this question

























  • possible duplicate of Grep: how to add an “OR” condition?

    – phuclv
    Nov 14 '17 at 2:26














541












541








541


192






I want to find all lines in several files that match one of two patterns. I tried to find the patterns I'm looking for by typing



grep (foo|bar) *.txt


but the shell interprets the | as a pipe and complains when bar isn't an executable.



How can I grep for multiple patterns in the same set of files?










share|improve this question
















I want to find all lines in several files that match one of two patterns. I tried to find the patterns I'm looking for by typing



grep (foo|bar) *.txt


but the shell interprets the | as a pipe and complains when bar isn't an executable.



How can I grep for multiple patterns in the same set of files?







shell grep regular-expression quoting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 11 '18 at 9:22









kenorb

8,856372110




8,856372110










asked Apr 26 '12 at 1:03









DanDan

3,36951734




3,36951734













  • possible duplicate of Grep: how to add an “OR” condition?

    – phuclv
    Nov 14 '17 at 2:26



















  • possible duplicate of Grep: how to add an “OR” condition?

    – phuclv
    Nov 14 '17 at 2:26

















possible duplicate of Grep: how to add an “OR” condition?

– phuclv
Nov 14 '17 at 2:26





possible duplicate of Grep: how to add an “OR” condition?

– phuclv
Nov 14 '17 at 2:26










12 Answers
12






active

oldest

votes


















764














First, you need to protect the pattern from expansion by the shell. The easiest way to do that is to put single quotes around it. Single quotes prevent expansion of anything between them (including backslashes); the only thing you can't do then is have single quotes in the pattern.



grep 'foo*' *.txt


If you do need a single quote, you can write it as ''' (end string literal, literal quote, open string literal).



grep 'foo*'''bar' *.txt


Second, grep supports two syntaxes for patterns. The old, default syntax (basic regular expressions) doesn't support the alternation (|) operator, though some versions have it as an extension, but written with a backslash.



grep 'foo|bar' *.txt


The portable way is to use the newer syntax, extended regular expressions. You need to pass the -E option to grep to select it. On Linux, you can also type egrep instead of grep -E (on other unices, you can make that an alias).



grep -E 'foo|bar' *.txt


Another possibility when you're just looking for any of several patterns (as opposed to building a complex pattern using disjunction) is to pass multiple patterns to grep. You can do this by preceding each pattern with the -e option.



grep -e foo -e bar *.txt





share|improve this answer





















  • 17





    As a sidenote -- when the patterns are fixed, you should really get into the habit of fgrep or grep -F, for small patterns the difference will be negligible but as they get longer, the benefits start to show...

    – TC1
    Apr 26 '12 at 9:37






  • 6





    @TC1 fgrep is deprecated according to man page

    – ramn
    Jul 22 '14 at 8:41






  • 16





    @TC1 Whether grep -F has an actual performance benefit depends on the grep implementation: some of them apply the same algorithm anyway, so that -F makes a difference only to the time spent parsing the pattern and not to the time searching. GNU grep isn't faster with -F, for example (it also has a bug that makes grep -F slower in multibyte locales — the same constant pattern with grep is actually significantly faster!). On the other hand BusyBox grep does benefit a lot from -F on large files.

    – Gilles
    Jul 22 '14 at 8:53








  • 4





    Perhaps it should be mentioned that for more complicated patterns where alternation is only to be for a part of the regular expression, it can be grouped with "(" and ")" (the escaping is for the default "basic regular expressions") (?).

    – Peter Mortensen
    May 20 '15 at 9:45








  • 4





    Note that egrep predates grep -E. It is not GNU specific (it certainly has nothing to do with Linux). Actually, you'll still find systems like Solaris where the default grep still doesn't support -E.

    – Stéphane Chazelas
    Jun 7 '16 at 11:27



















71














egrep "foo|bar" *.txt


or



grep "foo|bar" *.txt
grep -E "foo|bar" *.txt


selectively citing the man page of gnu-grep:



   -E, --extended-regexp
Interpret PATTERN as an extended regular expression (ERE, see below). (-E is specified by POSIX.)

Matching Control
-e PATTERN, --regexp=PATTERN
Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a pattern
beginning with a hyphen (-). (-e is specified by POSIX.)


(...)



   grep understands two different versions of regular expression syntax: “basic” and “extended.”  In  GNU grep,  there
is no difference in available functionality using either syntax. In other implementations, basic regular
expressions are less powerful. The following description applies to extended regular expressions; differences for
basic regular expressions are summarized afterwards.


In the beginning I didn't read further, so I didn't recognize the subtle differences:



Basic vs Extended Regular Expressions
In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the
backslashed versions ?, +, {, |, (, and ).


I always used egrep and needlessly parens, because I learned from examples. Now I learned something new. :)






share|improve this answer

































    20














    Like TC1 said, -F seems to be usable option:



    $> cat text
    some text
    foo
    another text
    bar
    end of file

    $> patterns="foo
    bar"

    $> grep -F "${patterns}" text
    foo
    bar





    share|improve this answer



















    • 1





      @poige I didn't know about the $'foonbar' option, not sure how expansion works here, need to look up, but thank you, that is really useful.

      – haridsv
      Nov 5 '12 at 12:26











    • Nice! This option also seems to make it run much faster (since it disables regex).

      – qwertzguy
      Jan 30 '18 at 0:44



















    13














    Firstly, you need to use quotes for special characters. Second, even so, grep will not understand alternation directly; you would need to use egrep, or (with GNU grep only) grep -E.



    egrep 'foo|bar' *.txt


    (The parentheses are unnecessary unless the alternation is part of a larger regex.)






    share|improve this answer





















    • 4





      Actually, grep -E is more standard than egrep.

      – jw013
      Apr 26 '12 at 1:14





















    7














    If you don't need regular expressions, it's much faster to use fgrep or grep -F with multiple -e parameters, like this:



    fgrep -efoo -ebar *.txt


    fgrep (alternatively grep -F) is much faster than regular grep because it searches for fixed strings instead of regular expressions.






    share|improve this answer



















    • 4





      Please see also the comments on this page mentioning that fgrep is deprecated.

      – phk
      Dec 27 '16 at 20:21



















    6














    You can try the below command to get the result:



    egrep 'rose.*lotus|lotus.*rose' some_file





    share|improve this answer

































      3














      I had access logs where the dates were stupidly formatted: [30/Jun/2013:08:00:45 +0200]



      But I needed to display it as: 30/Jun/2013 08:00:45



      The problem is that using "OR" in my grep statement, I was receiving the two match expressions on two separate lines.



      Here is the solution:



      grep -in myURL_of_interest  *access.log  | 
      grep -Eo '(b[[:digit:]]{2}/[[:upper:]][[:lower:]]{2}/[[:digit:]]{4}|[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}b)'
      | paste - - -d" " > MyAccess.log


      I hope it helps :)






      share|improve this answer

































        2














        A cheap and cheerful way to grep for multiple patterns:



        $ echo "foo" > ewq ; echo "bar" >> ewq ; grep -H -f ewq *.txt ; rm ewq





        share|improve this answer


























        • It could benefit from an explanation.

          – Peter Mortensen
          Dec 1 '17 at 4:12






        • 1





          The explanation is that grep's -f option takes a file with multiple patterns. Instead of creating a temporary file (that you may forget to delete afterwards), just use the shell's process substitution: grep -f <(echo foo; echo bar) *.txt

          – Jakob
          Mar 29 '18 at 8:03





















        2














        Pipe (|) is a special shell character, so it either needs to be escaped (|) or quoted as per manual (man bash):




        Quoting is used to remove the special meaning of certain characters or words to the shell. It can be used to disable special treatment for special characters, to prevent reserved words from being recognized as such, and to prevent parameter expansion.



        Enclosing characters in double quotes preserves the literal value of all characters within the quotes



        A non-quoted backslash () is the escape character.




        See: Which characters need to be escaped in Bash?



        Here are few examples (using tools not mentioned yet):





        • Using ripgrep:




          • rg "foo|bar" *.txt

          • rg -e foo -e bar *.txt




        • Using git grep:





          • git grep --no-index -e foo --or -e bar



            Note: It also supports Boolean expressions such as --and, --or and --not.






        For AND operation per line, see: How to run grep with multiple AND patterns?



        For AND operation per file, see: How to check all of multiple strings or regexes exist in a file?






        share|improve this answer

































          1














          This works for me



          root@gateway:/home/sshuser# aws ec2 describe-instances --instance-ids i-2db0459d |grep 'STATE|TAG'

          **STATE** 80 stopped

          **STATE**REASON Client.UserInitiatedShutdown Client.UserInitiatedShutdown: User initiated shutdown

          **TAGS** Name Magento-Testing root@gateway:/home/sshuser#





          share|improve this answer

































            1














            There are multiple ways to do so.




            1. grep 'foo|bar' *.txt

            2. egrep 'foo|bar' *.txt

            3. find . -maxdepth 1 -type f -name "*.txt" | xargs grep 'foo|bar'

            4. find . -maxdepth 1 -type f -name "*.txt" | xargs egrep 'foo|bar'


            3rd and 4th option will grep only in the files and avoid directories having .txt in their names.

            So, as per your use-case you can use any of the option mentioned above.

            Thanks!!





            share































              1














              TL;DR: if you want to do more things after matching one of the multiple patterns, enclose them as in (pattern1|pattern2)



              No one has mentioned searching for any one of multiple patterns followed by more patterns, so I figured I'd throw it out here:



              example: I want to find all the places where a variable that contains the name 'date' is defined as a String or int. (i.e., "int date =" or "String date ="):



              grep '.' -rne '(int|String) [a-zA-Z_]*date[a-zA-Z_]* ='





              share|improve this answer























                Your Answer








                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "106"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: false,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f37313%2fhow-do-i-grep-for-multiple-patterns-with-pattern-having-a-pipe-character%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                12 Answers
                12






                active

                oldest

                votes








                12 Answers
                12






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                764














                First, you need to protect the pattern from expansion by the shell. The easiest way to do that is to put single quotes around it. Single quotes prevent expansion of anything between them (including backslashes); the only thing you can't do then is have single quotes in the pattern.



                grep 'foo*' *.txt


                If you do need a single quote, you can write it as ''' (end string literal, literal quote, open string literal).



                grep 'foo*'''bar' *.txt


                Second, grep supports two syntaxes for patterns. The old, default syntax (basic regular expressions) doesn't support the alternation (|) operator, though some versions have it as an extension, but written with a backslash.



                grep 'foo|bar' *.txt


                The portable way is to use the newer syntax, extended regular expressions. You need to pass the -E option to grep to select it. On Linux, you can also type egrep instead of grep -E (on other unices, you can make that an alias).



                grep -E 'foo|bar' *.txt


                Another possibility when you're just looking for any of several patterns (as opposed to building a complex pattern using disjunction) is to pass multiple patterns to grep. You can do this by preceding each pattern with the -e option.



                grep -e foo -e bar *.txt





                share|improve this answer





















                • 17





                  As a sidenote -- when the patterns are fixed, you should really get into the habit of fgrep or grep -F, for small patterns the difference will be negligible but as they get longer, the benefits start to show...

                  – TC1
                  Apr 26 '12 at 9:37






                • 6





                  @TC1 fgrep is deprecated according to man page

                  – ramn
                  Jul 22 '14 at 8:41






                • 16





                  @TC1 Whether grep -F has an actual performance benefit depends on the grep implementation: some of them apply the same algorithm anyway, so that -F makes a difference only to the time spent parsing the pattern and not to the time searching. GNU grep isn't faster with -F, for example (it also has a bug that makes grep -F slower in multibyte locales — the same constant pattern with grep is actually significantly faster!). On the other hand BusyBox grep does benefit a lot from -F on large files.

                  – Gilles
                  Jul 22 '14 at 8:53








                • 4





                  Perhaps it should be mentioned that for more complicated patterns where alternation is only to be for a part of the regular expression, it can be grouped with "(" and ")" (the escaping is for the default "basic regular expressions") (?).

                  – Peter Mortensen
                  May 20 '15 at 9:45








                • 4





                  Note that egrep predates grep -E. It is not GNU specific (it certainly has nothing to do with Linux). Actually, you'll still find systems like Solaris where the default grep still doesn't support -E.

                  – Stéphane Chazelas
                  Jun 7 '16 at 11:27
















                764














                First, you need to protect the pattern from expansion by the shell. The easiest way to do that is to put single quotes around it. Single quotes prevent expansion of anything between them (including backslashes); the only thing you can't do then is have single quotes in the pattern.



                grep 'foo*' *.txt


                If you do need a single quote, you can write it as ''' (end string literal, literal quote, open string literal).



                grep 'foo*'''bar' *.txt


                Second, grep supports two syntaxes for patterns. The old, default syntax (basic regular expressions) doesn't support the alternation (|) operator, though some versions have it as an extension, but written with a backslash.



                grep 'foo|bar' *.txt


                The portable way is to use the newer syntax, extended regular expressions. You need to pass the -E option to grep to select it. On Linux, you can also type egrep instead of grep -E (on other unices, you can make that an alias).



                grep -E 'foo|bar' *.txt


                Another possibility when you're just looking for any of several patterns (as opposed to building a complex pattern using disjunction) is to pass multiple patterns to grep. You can do this by preceding each pattern with the -e option.



                grep -e foo -e bar *.txt





                share|improve this answer





















                • 17





                  As a sidenote -- when the patterns are fixed, you should really get into the habit of fgrep or grep -F, for small patterns the difference will be negligible but as they get longer, the benefits start to show...

                  – TC1
                  Apr 26 '12 at 9:37






                • 6





                  @TC1 fgrep is deprecated according to man page

                  – ramn
                  Jul 22 '14 at 8:41






                • 16





                  @TC1 Whether grep -F has an actual performance benefit depends on the grep implementation: some of them apply the same algorithm anyway, so that -F makes a difference only to the time spent parsing the pattern and not to the time searching. GNU grep isn't faster with -F, for example (it also has a bug that makes grep -F slower in multibyte locales — the same constant pattern with grep is actually significantly faster!). On the other hand BusyBox grep does benefit a lot from -F on large files.

                  – Gilles
                  Jul 22 '14 at 8:53








                • 4





                  Perhaps it should be mentioned that for more complicated patterns where alternation is only to be for a part of the regular expression, it can be grouped with "(" and ")" (the escaping is for the default "basic regular expressions") (?).

                  – Peter Mortensen
                  May 20 '15 at 9:45








                • 4





                  Note that egrep predates grep -E. It is not GNU specific (it certainly has nothing to do with Linux). Actually, you'll still find systems like Solaris where the default grep still doesn't support -E.

                  – Stéphane Chazelas
                  Jun 7 '16 at 11:27














                764












                764








                764







                First, you need to protect the pattern from expansion by the shell. The easiest way to do that is to put single quotes around it. Single quotes prevent expansion of anything between them (including backslashes); the only thing you can't do then is have single quotes in the pattern.



                grep 'foo*' *.txt


                If you do need a single quote, you can write it as ''' (end string literal, literal quote, open string literal).



                grep 'foo*'''bar' *.txt


                Second, grep supports two syntaxes for patterns. The old, default syntax (basic regular expressions) doesn't support the alternation (|) operator, though some versions have it as an extension, but written with a backslash.



                grep 'foo|bar' *.txt


                The portable way is to use the newer syntax, extended regular expressions. You need to pass the -E option to grep to select it. On Linux, you can also type egrep instead of grep -E (on other unices, you can make that an alias).



                grep -E 'foo|bar' *.txt


                Another possibility when you're just looking for any of several patterns (as opposed to building a complex pattern using disjunction) is to pass multiple patterns to grep. You can do this by preceding each pattern with the -e option.



                grep -e foo -e bar *.txt





                share|improve this answer















                First, you need to protect the pattern from expansion by the shell. The easiest way to do that is to put single quotes around it. Single quotes prevent expansion of anything between them (including backslashes); the only thing you can't do then is have single quotes in the pattern.



                grep 'foo*' *.txt


                If you do need a single quote, you can write it as ''' (end string literal, literal quote, open string literal).



                grep 'foo*'''bar' *.txt


                Second, grep supports two syntaxes for patterns. The old, default syntax (basic regular expressions) doesn't support the alternation (|) operator, though some versions have it as an extension, but written with a backslash.



                grep 'foo|bar' *.txt


                The portable way is to use the newer syntax, extended regular expressions. You need to pass the -E option to grep to select it. On Linux, you can also type egrep instead of grep -E (on other unices, you can make that an alias).



                grep -E 'foo|bar' *.txt


                Another possibility when you're just looking for any of several patterns (as opposed to building a complex pattern using disjunction) is to pass multiple patterns to grep. You can do this by preceding each pattern with the -e option.



                grep -e foo -e bar *.txt






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jun 7 '16 at 10:54









                Quentin Pradet

                1034




                1034










                answered Apr 26 '12 at 1:13









                GillesGilles

                542k12810961615




                542k12810961615








                • 17





                  As a sidenote -- when the patterns are fixed, you should really get into the habit of fgrep or grep -F, for small patterns the difference will be negligible but as they get longer, the benefits start to show...

                  – TC1
                  Apr 26 '12 at 9:37






                • 6





                  @TC1 fgrep is deprecated according to man page

                  – ramn
                  Jul 22 '14 at 8:41






                • 16





                  @TC1 Whether grep -F has an actual performance benefit depends on the grep implementation: some of them apply the same algorithm anyway, so that -F makes a difference only to the time spent parsing the pattern and not to the time searching. GNU grep isn't faster with -F, for example (it also has a bug that makes grep -F slower in multibyte locales — the same constant pattern with grep is actually significantly faster!). On the other hand BusyBox grep does benefit a lot from -F on large files.

                  – Gilles
                  Jul 22 '14 at 8:53








                • 4





                  Perhaps it should be mentioned that for more complicated patterns where alternation is only to be for a part of the regular expression, it can be grouped with "(" and ")" (the escaping is for the default "basic regular expressions") (?).

                  – Peter Mortensen
                  May 20 '15 at 9:45








                • 4





                  Note that egrep predates grep -E. It is not GNU specific (it certainly has nothing to do with Linux). Actually, you'll still find systems like Solaris where the default grep still doesn't support -E.

                  – Stéphane Chazelas
                  Jun 7 '16 at 11:27














                • 17





                  As a sidenote -- when the patterns are fixed, you should really get into the habit of fgrep or grep -F, for small patterns the difference will be negligible but as they get longer, the benefits start to show...

                  – TC1
                  Apr 26 '12 at 9:37






                • 6





                  @TC1 fgrep is deprecated according to man page

                  – ramn
                  Jul 22 '14 at 8:41






                • 16





                  @TC1 Whether grep -F has an actual performance benefit depends on the grep implementation: some of them apply the same algorithm anyway, so that -F makes a difference only to the time spent parsing the pattern and not to the time searching. GNU grep isn't faster with -F, for example (it also has a bug that makes grep -F slower in multibyte locales — the same constant pattern with grep is actually significantly faster!). On the other hand BusyBox grep does benefit a lot from -F on large files.

                  – Gilles
                  Jul 22 '14 at 8:53








                • 4





                  Perhaps it should be mentioned that for more complicated patterns where alternation is only to be for a part of the regular expression, it can be grouped with "(" and ")" (the escaping is for the default "basic regular expressions") (?).

                  – Peter Mortensen
                  May 20 '15 at 9:45








                • 4





                  Note that egrep predates grep -E. It is not GNU specific (it certainly has nothing to do with Linux). Actually, you'll still find systems like Solaris where the default grep still doesn't support -E.

                  – Stéphane Chazelas
                  Jun 7 '16 at 11:27








                17




                17





                As a sidenote -- when the patterns are fixed, you should really get into the habit of fgrep or grep -F, for small patterns the difference will be negligible but as they get longer, the benefits start to show...

                – TC1
                Apr 26 '12 at 9:37





                As a sidenote -- when the patterns are fixed, you should really get into the habit of fgrep or grep -F, for small patterns the difference will be negligible but as they get longer, the benefits start to show...

                – TC1
                Apr 26 '12 at 9:37




                6




                6





                @TC1 fgrep is deprecated according to man page

                – ramn
                Jul 22 '14 at 8:41





                @TC1 fgrep is deprecated according to man page

                – ramn
                Jul 22 '14 at 8:41




                16




                16





                @TC1 Whether grep -F has an actual performance benefit depends on the grep implementation: some of them apply the same algorithm anyway, so that -F makes a difference only to the time spent parsing the pattern and not to the time searching. GNU grep isn't faster with -F, for example (it also has a bug that makes grep -F slower in multibyte locales — the same constant pattern with grep is actually significantly faster!). On the other hand BusyBox grep does benefit a lot from -F on large files.

                – Gilles
                Jul 22 '14 at 8:53







                @TC1 Whether grep -F has an actual performance benefit depends on the grep implementation: some of them apply the same algorithm anyway, so that -F makes a difference only to the time spent parsing the pattern and not to the time searching. GNU grep isn't faster with -F, for example (it also has a bug that makes grep -F slower in multibyte locales — the same constant pattern with grep is actually significantly faster!). On the other hand BusyBox grep does benefit a lot from -F on large files.

                – Gilles
                Jul 22 '14 at 8:53






                4




                4





                Perhaps it should be mentioned that for more complicated patterns where alternation is only to be for a part of the regular expression, it can be grouped with "(" and ")" (the escaping is for the default "basic regular expressions") (?).

                – Peter Mortensen
                May 20 '15 at 9:45







                Perhaps it should be mentioned that for more complicated patterns where alternation is only to be for a part of the regular expression, it can be grouped with "(" and ")" (the escaping is for the default "basic regular expressions") (?).

                – Peter Mortensen
                May 20 '15 at 9:45






                4




                4





                Note that egrep predates grep -E. It is not GNU specific (it certainly has nothing to do with Linux). Actually, you'll still find systems like Solaris where the default grep still doesn't support -E.

                – Stéphane Chazelas
                Jun 7 '16 at 11:27





                Note that egrep predates grep -E. It is not GNU specific (it certainly has nothing to do with Linux). Actually, you'll still find systems like Solaris where the default grep still doesn't support -E.

                – Stéphane Chazelas
                Jun 7 '16 at 11:27













                71














                egrep "foo|bar" *.txt


                or



                grep "foo|bar" *.txt
                grep -E "foo|bar" *.txt


                selectively citing the man page of gnu-grep:



                   -E, --extended-regexp
                Interpret PATTERN as an extended regular expression (ERE, see below). (-E is specified by POSIX.)

                Matching Control
                -e PATTERN, --regexp=PATTERN
                Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a pattern
                beginning with a hyphen (-). (-e is specified by POSIX.)


                (...)



                   grep understands two different versions of regular expression syntax: “basic” and “extended.”  In  GNU grep,  there
                is no difference in available functionality using either syntax. In other implementations, basic regular
                expressions are less powerful. The following description applies to extended regular expressions; differences for
                basic regular expressions are summarized afterwards.


                In the beginning I didn't read further, so I didn't recognize the subtle differences:



                Basic vs Extended Regular Expressions
                In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the
                backslashed versions ?, +, {, |, (, and ).


                I always used egrep and needlessly parens, because I learned from examples. Now I learned something new. :)






                share|improve this answer






























                  71














                  egrep "foo|bar" *.txt


                  or



                  grep "foo|bar" *.txt
                  grep -E "foo|bar" *.txt


                  selectively citing the man page of gnu-grep:



                     -E, --extended-regexp
                  Interpret PATTERN as an extended regular expression (ERE, see below). (-E is specified by POSIX.)

                  Matching Control
                  -e PATTERN, --regexp=PATTERN
                  Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a pattern
                  beginning with a hyphen (-). (-e is specified by POSIX.)


                  (...)



                     grep understands two different versions of regular expression syntax: “basic” and “extended.”  In  GNU grep,  there
                  is no difference in available functionality using either syntax. In other implementations, basic regular
                  expressions are less powerful. The following description applies to extended regular expressions; differences for
                  basic regular expressions are summarized afterwards.


                  In the beginning I didn't read further, so I didn't recognize the subtle differences:



                  Basic vs Extended Regular Expressions
                  In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the
                  backslashed versions ?, +, {, |, (, and ).


                  I always used egrep and needlessly parens, because I learned from examples. Now I learned something new. :)






                  share|improve this answer




























                    71












                    71








                    71







                    egrep "foo|bar" *.txt


                    or



                    grep "foo|bar" *.txt
                    grep -E "foo|bar" *.txt


                    selectively citing the man page of gnu-grep:



                       -E, --extended-regexp
                    Interpret PATTERN as an extended regular expression (ERE, see below). (-E is specified by POSIX.)

                    Matching Control
                    -e PATTERN, --regexp=PATTERN
                    Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a pattern
                    beginning with a hyphen (-). (-e is specified by POSIX.)


                    (...)



                       grep understands two different versions of regular expression syntax: “basic” and “extended.”  In  GNU grep,  there
                    is no difference in available functionality using either syntax. In other implementations, basic regular
                    expressions are less powerful. The following description applies to extended regular expressions; differences for
                    basic regular expressions are summarized afterwards.


                    In the beginning I didn't read further, so I didn't recognize the subtle differences:



                    Basic vs Extended Regular Expressions
                    In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the
                    backslashed versions ?, +, {, |, (, and ).


                    I always used egrep and needlessly parens, because I learned from examples. Now I learned something new. :)






                    share|improve this answer















                    egrep "foo|bar" *.txt


                    or



                    grep "foo|bar" *.txt
                    grep -E "foo|bar" *.txt


                    selectively citing the man page of gnu-grep:



                       -E, --extended-regexp
                    Interpret PATTERN as an extended regular expression (ERE, see below). (-E is specified by POSIX.)

                    Matching Control
                    -e PATTERN, --regexp=PATTERN
                    Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a pattern
                    beginning with a hyphen (-). (-e is specified by POSIX.)


                    (...)



                       grep understands two different versions of regular expression syntax: “basic” and “extended.”  In  GNU grep,  there
                    is no difference in available functionality using either syntax. In other implementations, basic regular
                    expressions are less powerful. The following description applies to extended regular expressions; differences for
                    basic regular expressions are summarized afterwards.


                    In the beginning I didn't read further, so I didn't recognize the subtle differences:



                    Basic vs Extended Regular Expressions
                    In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the
                    backslashed versions ?, +, {, |, (, and ).


                    I always used egrep and needlessly parens, because I learned from examples. Now I learned something new. :)







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Sep 10 '15 at 11:18









                    terdon

                    132k32261441




                    132k32261441










                    answered Apr 26 '12 at 1:09









                    user unknownuser unknown

                    7,40112450




                    7,40112450























                        20














                        Like TC1 said, -F seems to be usable option:



                        $> cat text
                        some text
                        foo
                        another text
                        bar
                        end of file

                        $> patterns="foo
                        bar"

                        $> grep -F "${patterns}" text
                        foo
                        bar





                        share|improve this answer



















                        • 1





                          @poige I didn't know about the $'foonbar' option, not sure how expansion works here, need to look up, but thank you, that is really useful.

                          – haridsv
                          Nov 5 '12 at 12:26











                        • Nice! This option also seems to make it run much faster (since it disables regex).

                          – qwertzguy
                          Jan 30 '18 at 0:44
















                        20














                        Like TC1 said, -F seems to be usable option:



                        $> cat text
                        some text
                        foo
                        another text
                        bar
                        end of file

                        $> patterns="foo
                        bar"

                        $> grep -F "${patterns}" text
                        foo
                        bar





                        share|improve this answer



















                        • 1





                          @poige I didn't know about the $'foonbar' option, not sure how expansion works here, need to look up, but thank you, that is really useful.

                          – haridsv
                          Nov 5 '12 at 12:26











                        • Nice! This option also seems to make it run much faster (since it disables regex).

                          – qwertzguy
                          Jan 30 '18 at 0:44














                        20












                        20








                        20







                        Like TC1 said, -F seems to be usable option:



                        $> cat text
                        some text
                        foo
                        another text
                        bar
                        end of file

                        $> patterns="foo
                        bar"

                        $> grep -F "${patterns}" text
                        foo
                        bar





                        share|improve this answer













                        Like TC1 said, -F seems to be usable option:



                        $> cat text
                        some text
                        foo
                        another text
                        bar
                        end of file

                        $> patterns="foo
                        bar"

                        $> grep -F "${patterns}" text
                        foo
                        bar






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Apr 26 '12 at 18:46









                        ДМИТРИЙ МАЛИКОВДМИТРИЙ МАЛИКОВ

                        4,70652531




                        4,70652531








                        • 1





                          @poige I didn't know about the $'foonbar' option, not sure how expansion works here, need to look up, but thank you, that is really useful.

                          – haridsv
                          Nov 5 '12 at 12:26











                        • Nice! This option also seems to make it run much faster (since it disables regex).

                          – qwertzguy
                          Jan 30 '18 at 0:44














                        • 1





                          @poige I didn't know about the $'foonbar' option, not sure how expansion works here, need to look up, but thank you, that is really useful.

                          – haridsv
                          Nov 5 '12 at 12:26











                        • Nice! This option also seems to make it run much faster (since it disables regex).

                          – qwertzguy
                          Jan 30 '18 at 0:44








                        1




                        1





                        @poige I didn't know about the $'foonbar' option, not sure how expansion works here, need to look up, but thank you, that is really useful.

                        – haridsv
                        Nov 5 '12 at 12:26





                        @poige I didn't know about the $'foonbar' option, not sure how expansion works here, need to look up, but thank you, that is really useful.

                        – haridsv
                        Nov 5 '12 at 12:26













                        Nice! This option also seems to make it run much faster (since it disables regex).

                        – qwertzguy
                        Jan 30 '18 at 0:44





                        Nice! This option also seems to make it run much faster (since it disables regex).

                        – qwertzguy
                        Jan 30 '18 at 0:44











                        13














                        Firstly, you need to use quotes for special characters. Second, even so, grep will not understand alternation directly; you would need to use egrep, or (with GNU grep only) grep -E.



                        egrep 'foo|bar' *.txt


                        (The parentheses are unnecessary unless the alternation is part of a larger regex.)






                        share|improve this answer





















                        • 4





                          Actually, grep -E is more standard than egrep.

                          – jw013
                          Apr 26 '12 at 1:14


















                        13














                        Firstly, you need to use quotes for special characters. Second, even so, grep will not understand alternation directly; you would need to use egrep, or (with GNU grep only) grep -E.



                        egrep 'foo|bar' *.txt


                        (The parentheses are unnecessary unless the alternation is part of a larger regex.)






                        share|improve this answer





















                        • 4





                          Actually, grep -E is more standard than egrep.

                          – jw013
                          Apr 26 '12 at 1:14
















                        13












                        13








                        13







                        Firstly, you need to use quotes for special characters. Second, even so, grep will not understand alternation directly; you would need to use egrep, or (with GNU grep only) grep -E.



                        egrep 'foo|bar' *.txt


                        (The parentheses are unnecessary unless the alternation is part of a larger regex.)






                        share|improve this answer















                        Firstly, you need to use quotes for special characters. Second, even so, grep will not understand alternation directly; you would need to use egrep, or (with GNU grep only) grep -E.



                        egrep 'foo|bar' *.txt


                        (The parentheses are unnecessary unless the alternation is part of a larger regex.)







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited 5 mins ago









                        Prajwal Dhatwalia

                        1575




                        1575










                        answered Apr 26 '12 at 1:06









                        geekosaurgeekosaur

                        22.8k25953




                        22.8k25953








                        • 4





                          Actually, grep -E is more standard than egrep.

                          – jw013
                          Apr 26 '12 at 1:14
















                        • 4





                          Actually, grep -E is more standard than egrep.

                          – jw013
                          Apr 26 '12 at 1:14










                        4




                        4





                        Actually, grep -E is more standard than egrep.

                        – jw013
                        Apr 26 '12 at 1:14







                        Actually, grep -E is more standard than egrep.

                        – jw013
                        Apr 26 '12 at 1:14













                        7














                        If you don't need regular expressions, it's much faster to use fgrep or grep -F with multiple -e parameters, like this:



                        fgrep -efoo -ebar *.txt


                        fgrep (alternatively grep -F) is much faster than regular grep because it searches for fixed strings instead of regular expressions.






                        share|improve this answer



















                        • 4





                          Please see also the comments on this page mentioning that fgrep is deprecated.

                          – phk
                          Dec 27 '16 at 20:21
















                        7














                        If you don't need regular expressions, it's much faster to use fgrep or grep -F with multiple -e parameters, like this:



                        fgrep -efoo -ebar *.txt


                        fgrep (alternatively grep -F) is much faster than regular grep because it searches for fixed strings instead of regular expressions.






                        share|improve this answer



















                        • 4





                          Please see also the comments on this page mentioning that fgrep is deprecated.

                          – phk
                          Dec 27 '16 at 20:21














                        7












                        7








                        7







                        If you don't need regular expressions, it's much faster to use fgrep or grep -F with multiple -e parameters, like this:



                        fgrep -efoo -ebar *.txt


                        fgrep (alternatively grep -F) is much faster than regular grep because it searches for fixed strings instead of regular expressions.






                        share|improve this answer













                        If you don't need regular expressions, it's much faster to use fgrep or grep -F with multiple -e parameters, like this:



                        fgrep -efoo -ebar *.txt


                        fgrep (alternatively grep -F) is much faster than regular grep because it searches for fixed strings instead of regular expressions.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Dec 27 '16 at 20:02









                        Moustafa ElqabbanyMoustafa Elqabbany

                        17113




                        17113








                        • 4





                          Please see also the comments on this page mentioning that fgrep is deprecated.

                          – phk
                          Dec 27 '16 at 20:21














                        • 4





                          Please see also the comments on this page mentioning that fgrep is deprecated.

                          – phk
                          Dec 27 '16 at 20:21








                        4




                        4





                        Please see also the comments on this page mentioning that fgrep is deprecated.

                        – phk
                        Dec 27 '16 at 20:21





                        Please see also the comments on this page mentioning that fgrep is deprecated.

                        – phk
                        Dec 27 '16 at 20:21











                        6














                        You can try the below command to get the result:



                        egrep 'rose.*lotus|lotus.*rose' some_file





                        share|improve this answer






























                          6














                          You can try the below command to get the result:



                          egrep 'rose.*lotus|lotus.*rose' some_file





                          share|improve this answer




























                            6












                            6








                            6







                            You can try the below command to get the result:



                            egrep 'rose.*lotus|lotus.*rose' some_file





                            share|improve this answer















                            You can try the below command to get the result:



                            egrep 'rose.*lotus|lotus.*rose' some_file






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Dec 5 '16 at 15:01









                            HalosGhost

                            3,76392236




                            3,76392236










                            answered Dec 5 '16 at 14:39









                            AbhishekAbhishek

                            6111




                            6111























                                3














                                I had access logs where the dates were stupidly formatted: [30/Jun/2013:08:00:45 +0200]



                                But I needed to display it as: 30/Jun/2013 08:00:45



                                The problem is that using "OR" in my grep statement, I was receiving the two match expressions on two separate lines.



                                Here is the solution:



                                grep -in myURL_of_interest  *access.log  | 
                                grep -Eo '(b[[:digit:]]{2}/[[:upper:]][[:lower:]]{2}/[[:digit:]]{4}|[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}b)'
                                | paste - - -d" " > MyAccess.log


                                I hope it helps :)






                                share|improve this answer






























                                  3














                                  I had access logs where the dates were stupidly formatted: [30/Jun/2013:08:00:45 +0200]



                                  But I needed to display it as: 30/Jun/2013 08:00:45



                                  The problem is that using "OR" in my grep statement, I was receiving the two match expressions on two separate lines.



                                  Here is the solution:



                                  grep -in myURL_of_interest  *access.log  | 
                                  grep -Eo '(b[[:digit:]]{2}/[[:upper:]][[:lower:]]{2}/[[:digit:]]{4}|[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}b)'
                                  | paste - - -d" " > MyAccess.log


                                  I hope it helps :)






                                  share|improve this answer




























                                    3












                                    3








                                    3







                                    I had access logs where the dates were stupidly formatted: [30/Jun/2013:08:00:45 +0200]



                                    But I needed to display it as: 30/Jun/2013 08:00:45



                                    The problem is that using "OR" in my grep statement, I was receiving the two match expressions on two separate lines.



                                    Here is the solution:



                                    grep -in myURL_of_interest  *access.log  | 
                                    grep -Eo '(b[[:digit:]]{2}/[[:upper:]][[:lower:]]{2}/[[:digit:]]{4}|[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}b)'
                                    | paste - - -d" " > MyAccess.log


                                    I hope it helps :)






                                    share|improve this answer















                                    I had access logs where the dates were stupidly formatted: [30/Jun/2013:08:00:45 +0200]



                                    But I needed to display it as: 30/Jun/2013 08:00:45



                                    The problem is that using "OR" in my grep statement, I was receiving the two match expressions on two separate lines.



                                    Here is the solution:



                                    grep -in myURL_of_interest  *access.log  | 
                                    grep -Eo '(b[[:digit:]]{2}/[[:upper:]][[:lower:]]{2}/[[:digit:]]{4}|[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}b)'
                                    | paste - - -d" " > MyAccess.log


                                    I hope it helps :)







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Dec 1 '17 at 5:20









                                    Peter Mortensen

                                    91159




                                    91159










                                    answered Jul 15 '15 at 11:00









                                    tsmetstsmets

                                    312




                                    312























                                        2














                                        A cheap and cheerful way to grep for multiple patterns:



                                        $ echo "foo" > ewq ; echo "bar" >> ewq ; grep -H -f ewq *.txt ; rm ewq





                                        share|improve this answer


























                                        • It could benefit from an explanation.

                                          – Peter Mortensen
                                          Dec 1 '17 at 4:12






                                        • 1





                                          The explanation is that grep's -f option takes a file with multiple patterns. Instead of creating a temporary file (that you may forget to delete afterwards), just use the shell's process substitution: grep -f <(echo foo; echo bar) *.txt

                                          – Jakob
                                          Mar 29 '18 at 8:03


















                                        2














                                        A cheap and cheerful way to grep for multiple patterns:



                                        $ echo "foo" > ewq ; echo "bar" >> ewq ; grep -H -f ewq *.txt ; rm ewq





                                        share|improve this answer


























                                        • It could benefit from an explanation.

                                          – Peter Mortensen
                                          Dec 1 '17 at 4:12






                                        • 1





                                          The explanation is that grep's -f option takes a file with multiple patterns. Instead of creating a temporary file (that you may forget to delete afterwards), just use the shell's process substitution: grep -f <(echo foo; echo bar) *.txt

                                          – Jakob
                                          Mar 29 '18 at 8:03
















                                        2












                                        2








                                        2







                                        A cheap and cheerful way to grep for multiple patterns:



                                        $ echo "foo" > ewq ; echo "bar" >> ewq ; grep -H -f ewq *.txt ; rm ewq





                                        share|improve this answer















                                        A cheap and cheerful way to grep for multiple patterns:



                                        $ echo "foo" > ewq ; echo "bar" >> ewq ; grep -H -f ewq *.txt ; rm ewq






                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Feb 3 '17 at 16:32









                                        Stephen Rauch

                                        3,344101429




                                        3,344101429










                                        answered Feb 3 '17 at 16:17









                                        DHDHDHDDHDHDHD

                                        211




                                        211













                                        • It could benefit from an explanation.

                                          – Peter Mortensen
                                          Dec 1 '17 at 4:12






                                        • 1





                                          The explanation is that grep's -f option takes a file with multiple patterns. Instead of creating a temporary file (that you may forget to delete afterwards), just use the shell's process substitution: grep -f <(echo foo; echo bar) *.txt

                                          – Jakob
                                          Mar 29 '18 at 8:03





















                                        • It could benefit from an explanation.

                                          – Peter Mortensen
                                          Dec 1 '17 at 4:12






                                        • 1





                                          The explanation is that grep's -f option takes a file with multiple patterns. Instead of creating a temporary file (that you may forget to delete afterwards), just use the shell's process substitution: grep -f <(echo foo; echo bar) *.txt

                                          – Jakob
                                          Mar 29 '18 at 8:03



















                                        It could benefit from an explanation.

                                        – Peter Mortensen
                                        Dec 1 '17 at 4:12





                                        It could benefit from an explanation.

                                        – Peter Mortensen
                                        Dec 1 '17 at 4:12




                                        1




                                        1





                                        The explanation is that grep's -f option takes a file with multiple patterns. Instead of creating a temporary file (that you may forget to delete afterwards), just use the shell's process substitution: grep -f <(echo foo; echo bar) *.txt

                                        – Jakob
                                        Mar 29 '18 at 8:03







                                        The explanation is that grep's -f option takes a file with multiple patterns. Instead of creating a temporary file (that you may forget to delete afterwards), just use the shell's process substitution: grep -f <(echo foo; echo bar) *.txt

                                        – Jakob
                                        Mar 29 '18 at 8:03













                                        2














                                        Pipe (|) is a special shell character, so it either needs to be escaped (|) or quoted as per manual (man bash):




                                        Quoting is used to remove the special meaning of certain characters or words to the shell. It can be used to disable special treatment for special characters, to prevent reserved words from being recognized as such, and to prevent parameter expansion.



                                        Enclosing characters in double quotes preserves the literal value of all characters within the quotes



                                        A non-quoted backslash () is the escape character.




                                        See: Which characters need to be escaped in Bash?



                                        Here are few examples (using tools not mentioned yet):





                                        • Using ripgrep:




                                          • rg "foo|bar" *.txt

                                          • rg -e foo -e bar *.txt




                                        • Using git grep:





                                          • git grep --no-index -e foo --or -e bar



                                            Note: It also supports Boolean expressions such as --and, --or and --not.






                                        For AND operation per line, see: How to run grep with multiple AND patterns?



                                        For AND operation per file, see: How to check all of multiple strings or regexes exist in a file?






                                        share|improve this answer






























                                          2














                                          Pipe (|) is a special shell character, so it either needs to be escaped (|) or quoted as per manual (man bash):




                                          Quoting is used to remove the special meaning of certain characters or words to the shell. It can be used to disable special treatment for special characters, to prevent reserved words from being recognized as such, and to prevent parameter expansion.



                                          Enclosing characters in double quotes preserves the literal value of all characters within the quotes



                                          A non-quoted backslash () is the escape character.




                                          See: Which characters need to be escaped in Bash?



                                          Here are few examples (using tools not mentioned yet):





                                          • Using ripgrep:




                                            • rg "foo|bar" *.txt

                                            • rg -e foo -e bar *.txt




                                          • Using git grep:





                                            • git grep --no-index -e foo --or -e bar



                                              Note: It also supports Boolean expressions such as --and, --or and --not.






                                          For AND operation per line, see: How to run grep with multiple AND patterns?



                                          For AND operation per file, see: How to check all of multiple strings or regexes exist in a file?






                                          share|improve this answer




























                                            2












                                            2








                                            2







                                            Pipe (|) is a special shell character, so it either needs to be escaped (|) or quoted as per manual (man bash):




                                            Quoting is used to remove the special meaning of certain characters or words to the shell. It can be used to disable special treatment for special characters, to prevent reserved words from being recognized as such, and to prevent parameter expansion.



                                            Enclosing characters in double quotes preserves the literal value of all characters within the quotes



                                            A non-quoted backslash () is the escape character.




                                            See: Which characters need to be escaped in Bash?



                                            Here are few examples (using tools not mentioned yet):





                                            • Using ripgrep:




                                              • rg "foo|bar" *.txt

                                              • rg -e foo -e bar *.txt




                                            • Using git grep:





                                              • git grep --no-index -e foo --or -e bar



                                                Note: It also supports Boolean expressions such as --and, --or and --not.






                                            For AND operation per line, see: How to run grep with multiple AND patterns?



                                            For AND operation per file, see: How to check all of multiple strings or regexes exist in a file?






                                            share|improve this answer















                                            Pipe (|) is a special shell character, so it either needs to be escaped (|) or quoted as per manual (man bash):




                                            Quoting is used to remove the special meaning of certain characters or words to the shell. It can be used to disable special treatment for special characters, to prevent reserved words from being recognized as such, and to prevent parameter expansion.



                                            Enclosing characters in double quotes preserves the literal value of all characters within the quotes



                                            A non-quoted backslash () is the escape character.




                                            See: Which characters need to be escaped in Bash?



                                            Here are few examples (using tools not mentioned yet):





                                            • Using ripgrep:




                                              • rg "foo|bar" *.txt

                                              • rg -e foo -e bar *.txt




                                            • Using git grep:





                                              • git grep --no-index -e foo --or -e bar



                                                Note: It also supports Boolean expressions such as --and, --or and --not.






                                            For AND operation per line, see: How to run grep with multiple AND patterns?



                                            For AND operation per file, see: How to check all of multiple strings or regexes exist in a file?







                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited Apr 19 '18 at 18:09

























                                            answered Apr 11 '18 at 9:41









                                            kenorbkenorb

                                            8,856372110




                                            8,856372110























                                                1














                                                This works for me



                                                root@gateway:/home/sshuser# aws ec2 describe-instances --instance-ids i-2db0459d |grep 'STATE|TAG'

                                                **STATE** 80 stopped

                                                **STATE**REASON Client.UserInitiatedShutdown Client.UserInitiatedShutdown: User initiated shutdown

                                                **TAGS** Name Magento-Testing root@gateway:/home/sshuser#





                                                share|improve this answer






























                                                  1














                                                  This works for me



                                                  root@gateway:/home/sshuser# aws ec2 describe-instances --instance-ids i-2db0459d |grep 'STATE|TAG'

                                                  **STATE** 80 stopped

                                                  **STATE**REASON Client.UserInitiatedShutdown Client.UserInitiatedShutdown: User initiated shutdown

                                                  **TAGS** Name Magento-Testing root@gateway:/home/sshuser#





                                                  share|improve this answer




























                                                    1












                                                    1








                                                    1







                                                    This works for me



                                                    root@gateway:/home/sshuser# aws ec2 describe-instances --instance-ids i-2db0459d |grep 'STATE|TAG'

                                                    **STATE** 80 stopped

                                                    **STATE**REASON Client.UserInitiatedShutdown Client.UserInitiatedShutdown: User initiated shutdown

                                                    **TAGS** Name Magento-Testing root@gateway:/home/sshuser#





                                                    share|improve this answer















                                                    This works for me



                                                    root@gateway:/home/sshuser# aws ec2 describe-instances --instance-ids i-2db0459d |grep 'STATE|TAG'

                                                    **STATE** 80 stopped

                                                    **STATE**REASON Client.UserInitiatedShutdown Client.UserInitiatedShutdown: User initiated shutdown

                                                    **TAGS** Name Magento-Testing root@gateway:/home/sshuser#






                                                    share|improve this answer














                                                    share|improve this answer



                                                    share|improve this answer








                                                    edited Jul 27 '16 at 17:24









                                                    HalosGhost

                                                    3,76392236




                                                    3,76392236










                                                    answered Jul 27 '16 at 5:13









                                                    Mansur AliMansur Ali

                                                    1853




                                                    1853























                                                        1














                                                        There are multiple ways to do so.




                                                        1. grep 'foo|bar' *.txt

                                                        2. egrep 'foo|bar' *.txt

                                                        3. find . -maxdepth 1 -type f -name "*.txt" | xargs grep 'foo|bar'

                                                        4. find . -maxdepth 1 -type f -name "*.txt" | xargs egrep 'foo|bar'


                                                        3rd and 4th option will grep only in the files and avoid directories having .txt in their names.

                                                        So, as per your use-case you can use any of the option mentioned above.

                                                        Thanks!!





                                                        share




























                                                          1














                                                          There are multiple ways to do so.




                                                          1. grep 'foo|bar' *.txt

                                                          2. egrep 'foo|bar' *.txt

                                                          3. find . -maxdepth 1 -type f -name "*.txt" | xargs grep 'foo|bar'

                                                          4. find . -maxdepth 1 -type f -name "*.txt" | xargs egrep 'foo|bar'


                                                          3rd and 4th option will grep only in the files and avoid directories having .txt in their names.

                                                          So, as per your use-case you can use any of the option mentioned above.

                                                          Thanks!!





                                                          share


























                                                            1












                                                            1








                                                            1







                                                            There are multiple ways to do so.




                                                            1. grep 'foo|bar' *.txt

                                                            2. egrep 'foo|bar' *.txt

                                                            3. find . -maxdepth 1 -type f -name "*.txt" | xargs grep 'foo|bar'

                                                            4. find . -maxdepth 1 -type f -name "*.txt" | xargs egrep 'foo|bar'


                                                            3rd and 4th option will grep only in the files and avoid directories having .txt in their names.

                                                            So, as per your use-case you can use any of the option mentioned above.

                                                            Thanks!!





                                                            share













                                                            There are multiple ways to do so.




                                                            1. grep 'foo|bar' *.txt

                                                            2. egrep 'foo|bar' *.txt

                                                            3. find . -maxdepth 1 -type f -name "*.txt" | xargs grep 'foo|bar'

                                                            4. find . -maxdepth 1 -type f -name "*.txt" | xargs egrep 'foo|bar'


                                                            3rd and 4th option will grep only in the files and avoid directories having .txt in their names.

                                                            So, as per your use-case you can use any of the option mentioned above.

                                                            Thanks!!






                                                            share











                                                            share


                                                            share










                                                            answered Apr 10 '18 at 5:51









                                                            Bhagyesh DudhediyaBhagyesh Dudhediya

                                                            351416




                                                            351416























                                                                1














                                                                TL;DR: if you want to do more things after matching one of the multiple patterns, enclose them as in (pattern1|pattern2)



                                                                No one has mentioned searching for any one of multiple patterns followed by more patterns, so I figured I'd throw it out here:



                                                                example: I want to find all the places where a variable that contains the name 'date' is defined as a String or int. (i.e., "int date =" or "String date ="):



                                                                grep '.' -rne '(int|String) [a-zA-Z_]*date[a-zA-Z_]* ='





                                                                share|improve this answer




























                                                                  1














                                                                  TL;DR: if you want to do more things after matching one of the multiple patterns, enclose them as in (pattern1|pattern2)



                                                                  No one has mentioned searching for any one of multiple patterns followed by more patterns, so I figured I'd throw it out here:



                                                                  example: I want to find all the places where a variable that contains the name 'date' is defined as a String or int. (i.e., "int date =" or "String date ="):



                                                                  grep '.' -rne '(int|String) [a-zA-Z_]*date[a-zA-Z_]* ='





                                                                  share|improve this answer


























                                                                    1












                                                                    1








                                                                    1







                                                                    TL;DR: if you want to do more things after matching one of the multiple patterns, enclose them as in (pattern1|pattern2)



                                                                    No one has mentioned searching for any one of multiple patterns followed by more patterns, so I figured I'd throw it out here:



                                                                    example: I want to find all the places where a variable that contains the name 'date' is defined as a String or int. (i.e., "int date =" or "String date ="):



                                                                    grep '.' -rne '(int|String) [a-zA-Z_]*date[a-zA-Z_]* ='





                                                                    share|improve this answer













                                                                    TL;DR: if you want to do more things after matching one of the multiple patterns, enclose them as in (pattern1|pattern2)



                                                                    No one has mentioned searching for any one of multiple patterns followed by more patterns, so I figured I'd throw it out here:



                                                                    example: I want to find all the places where a variable that contains the name 'date' is defined as a String or int. (i.e., "int date =" or "String date ="):



                                                                    grep '.' -rne '(int|String) [a-zA-Z_]*date[a-zA-Z_]* ='






                                                                    share|improve this answer












                                                                    share|improve this answer



                                                                    share|improve this answer










                                                                    answered Dec 10 '18 at 20:03









                                                                    jeremysprofilejeremysprofile

                                                                    256111




                                                                    256111






























                                                                        draft saved

                                                                        draft discarded




















































                                                                        Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                                                        • Please be sure to answer the question. Provide details and share your research!

                                                                        But avoid



                                                                        • Asking for help, clarification, or responding to other answers.

                                                                        • Making statements based on opinion; back them up with references or personal experience.


                                                                        To learn more, see our tips on writing great answers.




                                                                        draft saved


                                                                        draft discarded














                                                                        StackExchange.ready(
                                                                        function () {
                                                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f37313%2fhow-do-i-grep-for-multiple-patterns-with-pattern-having-a-pipe-character%23new-answer', 'question_page');
                                                                        }
                                                                        );

                                                                        Post as a guest















                                                                        Required, but never shown





















































                                                                        Required, but never shown














                                                                        Required, but never shown












                                                                        Required, but never shown







                                                                        Required, but never shown

































                                                                        Required, but never shown














                                                                        Required, but never shown












                                                                        Required, but never shown







                                                                        Required, but never shown







                                                                        Popular posts from this blog

                                                                        CARDNET

                                                                        Boot-repair Failure: Unable to locate package grub-common:i386

                                                                        濃尾地震