How to use grep to search word with specific number of occurence

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












How can I search a text in a file that matches such condition: starts with 'x' and and have at least 1 'a' immediately after the 'x' and at most 2 'a's immediately following the 'a'.



I tried something like



grep -i ^xa1..2 textfile.txt


but I think I am not understanding the usage of it










share|improve this question





















  • so basically, you are looking for the pattern "xaaa" ?
    – Alchemist
    Oct 9 '17 at 3:18










  • You should add some examples of text input and an an example of the expected result. It looks like you don't want to match xaaaax. Please clearify by editing your question.
    – hschou
    Oct 9 '17 at 6:03















up vote
0
down vote

favorite












How can I search a text in a file that matches such condition: starts with 'x' and and have at least 1 'a' immediately after the 'x' and at most 2 'a's immediately following the 'a'.



I tried something like



grep -i ^xa1..2 textfile.txt


but I think I am not understanding the usage of it










share|improve this question





















  • so basically, you are looking for the pattern "xaaa" ?
    – Alchemist
    Oct 9 '17 at 3:18










  • You should add some examples of text input and an an example of the expected result. It looks like you don't want to match xaaaax. Please clearify by editing your question.
    – hschou
    Oct 9 '17 at 6:03













up vote
0
down vote

favorite









up vote
0
down vote

favorite











How can I search a text in a file that matches such condition: starts with 'x' and and have at least 1 'a' immediately after the 'x' and at most 2 'a's immediately following the 'a'.



I tried something like



grep -i ^xa1..2 textfile.txt


but I think I am not understanding the usage of it










share|improve this question













How can I search a text in a file that matches such condition: starts with 'x' and and have at least 1 'a' immediately after the 'x' and at most 2 'a's immediately following the 'a'.



I tried something like



grep -i ^xa1..2 textfile.txt


but I think I am not understanding the usage of it







grep






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Oct 9 '17 at 3:12









user254679

1




1











  • so basically, you are looking for the pattern "xaaa" ?
    – Alchemist
    Oct 9 '17 at 3:18










  • You should add some examples of text input and an an example of the expected result. It looks like you don't want to match xaaaax. Please clearify by editing your question.
    – hschou
    Oct 9 '17 at 6:03

















  • so basically, you are looking for the pattern "xaaa" ?
    – Alchemist
    Oct 9 '17 at 3:18










  • You should add some examples of text input and an an example of the expected result. It looks like you don't want to match xaaaax. Please clearify by editing your question.
    – hschou
    Oct 9 '17 at 6:03
















so basically, you are looking for the pattern "xaaa" ?
– Alchemist
Oct 9 '17 at 3:18




so basically, you are looking for the pattern "xaaa" ?
– Alchemist
Oct 9 '17 at 3:18












You should add some examples of text input and an an example of the expected result. It looks like you don't want to match xaaaax. Please clearify by editing your question.
– hschou
Oct 9 '17 at 6:03





You should add some examples of text input and an an example of the expected result. It looks like you don't want to match xaaaax. Please clearify by editing your question.
– hschou
Oct 9 '17 at 6:03











3 Answers
3






active

oldest

votes

















up vote
1
down vote













$ cat ip.txt 
xe
xa
xaaaalt
xaaa
xaa

$ # match 'a' 1 to 3 times
$ grep -i '^xa1,3' ip.txt
xa
xaaaalt
xaaa
xaa

$ # with ERE, no need to escape
$ grep -iE '^xa1,3' ip.txt
xa
xaaaalt
xaaa
xaa


To prevent matching more than three consecutive a matching, you'd need



$ grep -iE '^xa1,3([^a]|$)' ip.txt
xa
xaaa
xaa

$ # or lookarounds with PCRE
$ grep -iP '^xa1,3(?!a)' ip.txt
xa
xaaa
xaa





share|improve this answer





























    up vote
    0
    down vote













    You were close:



    grep -i -E '^xaa0,2' textfile.txt


    The syntax n,m (with , and not ..) is an extended regexp, hence the -E option. and need to be quoted so that the shell doesn't interpret them.






    share|improve this answer



























      up vote
      0
      down vote













      Given the input



      1 x
      2 xa
      3 xaa
      4 xaaa
      5 xaaaa


      Lines 2, 3 and 4 will be returned by



      grep -wE 'xa1,3'


      The extended regular expression xa1,3 will match any x followed by between 1 and 3 a. The -w option to grep makes sure that the matching text in the input is a complete word (which is why line 5 is not returned).






      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',
        convertImagesToLinks: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        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%2f396926%2fhow-to-use-grep-to-search-word-with-specific-number-of-occurence%23new-answer', 'question_page');

        );

        Post as a guest






























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        1
        down vote













        $ cat ip.txt 
        xe
        xa
        xaaaalt
        xaaa
        xaa

        $ # match 'a' 1 to 3 times
        $ grep -i '^xa1,3' ip.txt
        xa
        xaaaalt
        xaaa
        xaa

        $ # with ERE, no need to escape
        $ grep -iE '^xa1,3' ip.txt
        xa
        xaaaalt
        xaaa
        xaa


        To prevent matching more than three consecutive a matching, you'd need



        $ grep -iE '^xa1,3([^a]|$)' ip.txt
        xa
        xaaa
        xaa

        $ # or lookarounds with PCRE
        $ grep -iP '^xa1,3(?!a)' ip.txt
        xa
        xaaa
        xaa





        share|improve this answer


























          up vote
          1
          down vote













          $ cat ip.txt 
          xe
          xa
          xaaaalt
          xaaa
          xaa

          $ # match 'a' 1 to 3 times
          $ grep -i '^xa1,3' ip.txt
          xa
          xaaaalt
          xaaa
          xaa

          $ # with ERE, no need to escape
          $ grep -iE '^xa1,3' ip.txt
          xa
          xaaaalt
          xaaa
          xaa


          To prevent matching more than three consecutive a matching, you'd need



          $ grep -iE '^xa1,3([^a]|$)' ip.txt
          xa
          xaaa
          xaa

          $ # or lookarounds with PCRE
          $ grep -iP '^xa1,3(?!a)' ip.txt
          xa
          xaaa
          xaa





          share|improve this answer
























            up vote
            1
            down vote










            up vote
            1
            down vote









            $ cat ip.txt 
            xe
            xa
            xaaaalt
            xaaa
            xaa

            $ # match 'a' 1 to 3 times
            $ grep -i '^xa1,3' ip.txt
            xa
            xaaaalt
            xaaa
            xaa

            $ # with ERE, no need to escape
            $ grep -iE '^xa1,3' ip.txt
            xa
            xaaaalt
            xaaa
            xaa


            To prevent matching more than three consecutive a matching, you'd need



            $ grep -iE '^xa1,3([^a]|$)' ip.txt
            xa
            xaaa
            xaa

            $ # or lookarounds with PCRE
            $ grep -iP '^xa1,3(?!a)' ip.txt
            xa
            xaaa
            xaa





            share|improve this answer














            $ cat ip.txt 
            xe
            xa
            xaaaalt
            xaaa
            xaa

            $ # match 'a' 1 to 3 times
            $ grep -i '^xa1,3' ip.txt
            xa
            xaaaalt
            xaaa
            xaa

            $ # with ERE, no need to escape
            $ grep -iE '^xa1,3' ip.txt
            xa
            xaaaalt
            xaaa
            xaa


            To prevent matching more than three consecutive a matching, you'd need



            $ grep -iE '^xa1,3([^a]|$)' ip.txt
            xa
            xaaa
            xaa

            $ # or lookarounds with PCRE
            $ grep -iP '^xa1,3(?!a)' ip.txt
            xa
            xaaa
            xaa






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 9 '17 at 4:12

























            answered Oct 9 '17 at 4:06









            Sundeep

            6,9611826




            6,9611826






















                up vote
                0
                down vote













                You were close:



                grep -i -E '^xaa0,2' textfile.txt


                The syntax n,m (with , and not ..) is an extended regexp, hence the -E option. and need to be quoted so that the shell doesn't interpret them.






                share|improve this answer
























                  up vote
                  0
                  down vote













                  You were close:



                  grep -i -E '^xaa0,2' textfile.txt


                  The syntax n,m (with , and not ..) is an extended regexp, hence the -E option. and need to be quoted so that the shell doesn't interpret them.






                  share|improve this answer






















                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    You were close:



                    grep -i -E '^xaa0,2' textfile.txt


                    The syntax n,m (with , and not ..) is an extended regexp, hence the -E option. and need to be quoted so that the shell doesn't interpret them.






                    share|improve this answer












                    You were close:



                    grep -i -E '^xaa0,2' textfile.txt


                    The syntax n,m (with , and not ..) is an extended regexp, hence the -E option. and need to be quoted so that the shell doesn't interpret them.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Oct 9 '17 at 3:18









                    xhienne

                    11.7k2553




                    11.7k2553




















                        up vote
                        0
                        down vote













                        Given the input



                        1 x
                        2 xa
                        3 xaa
                        4 xaaa
                        5 xaaaa


                        Lines 2, 3 and 4 will be returned by



                        grep -wE 'xa1,3'


                        The extended regular expression xa1,3 will match any x followed by between 1 and 3 a. The -w option to grep makes sure that the matching text in the input is a complete word (which is why line 5 is not returned).






                        share|improve this answer
























                          up vote
                          0
                          down vote













                          Given the input



                          1 x
                          2 xa
                          3 xaa
                          4 xaaa
                          5 xaaaa


                          Lines 2, 3 and 4 will be returned by



                          grep -wE 'xa1,3'


                          The extended regular expression xa1,3 will match any x followed by between 1 and 3 a. The -w option to grep makes sure that the matching text in the input is a complete word (which is why line 5 is not returned).






                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            Given the input



                            1 x
                            2 xa
                            3 xaa
                            4 xaaa
                            5 xaaaa


                            Lines 2, 3 and 4 will be returned by



                            grep -wE 'xa1,3'


                            The extended regular expression xa1,3 will match any x followed by between 1 and 3 a. The -w option to grep makes sure that the matching text in the input is a complete word (which is why line 5 is not returned).






                            share|improve this answer












                            Given the input



                            1 x
                            2 xa
                            3 xaa
                            4 xaaa
                            5 xaaaa


                            Lines 2, 3 and 4 will be returned by



                            grep -wE 'xa1,3'


                            The extended regular expression xa1,3 will match any x followed by between 1 and 3 a. The -w option to grep makes sure that the matching text in the input is a complete word (which is why line 5 is not returned).







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Oct 9 '17 at 5:57









                            Kusalananda

                            105k14209326




                            105k14209326



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f396926%2fhow-to-use-grep-to-search-word-with-specific-number-of-occurence%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Popular posts from this blog

                                How to check contact read email or not when send email to Individual?

                                Bahrain

                                Postfix configuration issue with fips on centos 7; mailgun relay