How to grep lines which have more than specific number of special characters

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











up vote
1
down vote

favorite












i would like to know what's the best way to grep lines from text which have more than specific number of special characters.



Let's say that you already know that each line have 4 comma , and you would like to grep lines which have more than 4 comma ,



Example



hi,hello,how,are,you
catch,me,then,say,hello,then


Output



catch,me,then,say,hello,then






share|improve this question






















  • grep -E '(.*,)5' file?
    – Cyrus
    Dec 22 '17 at 10:04










  • @Cyrus thank you and in case if you wanna grep what is less than 4 ?
    – Î±Ô‹É±Ò½Ôƒ αмєяιcαη
    Dec 22 '17 at 10:06










  • also go ahead and write your comment as answer so i gonna accept it as answer.
    – Î±Ô‹É±Ò½Ôƒ αмєяιcαη
    Dec 22 '17 at 10:07










  • grep -E '^([^,]*,),3[^,]*$' file? This might help: The Stack Overflow Regular Expressions FAQ
    – Cyrus
    Dec 22 '17 at 10:19















up vote
1
down vote

favorite












i would like to know what's the best way to grep lines from text which have more than specific number of special characters.



Let's say that you already know that each line have 4 comma , and you would like to grep lines which have more than 4 comma ,



Example



hi,hello,how,are,you
catch,me,then,say,hello,then


Output



catch,me,then,say,hello,then






share|improve this question






















  • grep -E '(.*,)5' file?
    – Cyrus
    Dec 22 '17 at 10:04










  • @Cyrus thank you and in case if you wanna grep what is less than 4 ?
    – Î±Ô‹É±Ò½Ôƒ αмєяιcαη
    Dec 22 '17 at 10:06










  • also go ahead and write your comment as answer so i gonna accept it as answer.
    – Î±Ô‹É±Ò½Ôƒ αмєяιcαη
    Dec 22 '17 at 10:07










  • grep -E '^([^,]*,),3[^,]*$' file? This might help: The Stack Overflow Regular Expressions FAQ
    – Cyrus
    Dec 22 '17 at 10:19













up vote
1
down vote

favorite









up vote
1
down vote

favorite











i would like to know what's the best way to grep lines from text which have more than specific number of special characters.



Let's say that you already know that each line have 4 comma , and you would like to grep lines which have more than 4 comma ,



Example



hi,hello,how,are,you
catch,me,then,say,hello,then


Output



catch,me,then,say,hello,then






share|improve this question














i would like to know what's the best way to grep lines from text which have more than specific number of special characters.



Let's say that you already know that each line have 4 comma , and you would like to grep lines which have more than 4 comma ,



Example



hi,hello,how,are,you
catch,me,then,say,hello,then


Output



catch,me,then,say,hello,then








share|improve this question













share|improve this question




share|improve this question








edited Dec 22 '17 at 10:07









choroba

24.4k34067




24.4k34067










asked Dec 22 '17 at 10:00









αԋɱҽԃ αмєяιcαη

407418




407418











  • grep -E '(.*,)5' file?
    – Cyrus
    Dec 22 '17 at 10:04










  • @Cyrus thank you and in case if you wanna grep what is less than 4 ?
    – Î±Ô‹É±Ò½Ôƒ αмєяιcαη
    Dec 22 '17 at 10:06










  • also go ahead and write your comment as answer so i gonna accept it as answer.
    – Î±Ô‹É±Ò½Ôƒ αмєяιcαη
    Dec 22 '17 at 10:07










  • grep -E '^([^,]*,),3[^,]*$' file? This might help: The Stack Overflow Regular Expressions FAQ
    – Cyrus
    Dec 22 '17 at 10:19

















  • grep -E '(.*,)5' file?
    – Cyrus
    Dec 22 '17 at 10:04










  • @Cyrus thank you and in case if you wanna grep what is less than 4 ?
    – Î±Ô‹É±Ò½Ôƒ αмєяιcαη
    Dec 22 '17 at 10:06










  • also go ahead and write your comment as answer so i gonna accept it as answer.
    – Î±Ô‹É±Ò½Ôƒ αмєяιcαη
    Dec 22 '17 at 10:07










  • grep -E '^([^,]*,),3[^,]*$' file? This might help: The Stack Overflow Regular Expressions FAQ
    – Cyrus
    Dec 22 '17 at 10:19
















grep -E '(.*,)5' file?
– Cyrus
Dec 22 '17 at 10:04




grep -E '(.*,)5' file?
– Cyrus
Dec 22 '17 at 10:04












@Cyrus thank you and in case if you wanna grep what is less than 4 ?
– Î±Ô‹É±Ò½Ôƒ αмєяιcαη
Dec 22 '17 at 10:06




@Cyrus thank you and in case if you wanna grep what is less than 4 ?
– Î±Ô‹É±Ò½Ôƒ αмєяιcαη
Dec 22 '17 at 10:06












also go ahead and write your comment as answer so i gonna accept it as answer.
– Î±Ô‹É±Ò½Ôƒ αмєяιcαη
Dec 22 '17 at 10:07




also go ahead and write your comment as answer so i gonna accept it as answer.
– Î±Ô‹É±Ò½Ôƒ αмєяιcαη
Dec 22 '17 at 10:07












grep -E '^([^,]*,),3[^,]*$' file? This might help: The Stack Overflow Regular Expressions FAQ
– Cyrus
Dec 22 '17 at 10:19





grep -E '^([^,]*,),3[^,]*$' file? This might help: The Stack Overflow Regular Expressions FAQ
– Cyrus
Dec 22 '17 at 10:19











4 Answers
4






active

oldest

votes

















up vote
3
down vote



accepted










Perl solution:



perl -ne 'print if tr/,// > 4'



  • -n reads the file line by line

  • the tr operator returns the number of matches.

To print the lines with less than 4, just change > to <.






share|improve this answer




















  • That's brilliant solution which i highly like .
    – Î±Ô‹É±Ò½Ôƒ αмєяιcαη
    Dec 22 '17 at 10:11

















up vote
1
down vote













Using the grep command:



grep -E '(,.*)5,' myfile


does the job. Explanation:



-E: use an Extended Regex...



'(,.*): ... to find one comma followed by any number of characters, even zero...



5,': ... and repeat the previous pattern 5 times or more.



If you want to grep lines with less than 4 commas, replace 5,' with 0,3'.






share|improve this answer





























    up vote
    1
    down vote













    The awk version:



    awk -F, 'NF > 5' myfile





    share|improve this answer



























      up vote
      -1
      down vote













      Achieved result by below one liner




      l=`awk 'BEGINprint print gsub(",","")' example.txt |sed '/^$/d' |awk '$1 > 4 print NR'`;sed -n ''$l'p' example.txt 



      output
      catch,me,then,say,hello,then





      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%2f412461%2fhow-to-grep-lines-which-have-more-than-specific-number-of-special-characters%23new-answer', 'question_page');

        );

        Post as a guest






























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        3
        down vote



        accepted










        Perl solution:



        perl -ne 'print if tr/,// > 4'



        • -n reads the file line by line

        • the tr operator returns the number of matches.

        To print the lines with less than 4, just change > to <.






        share|improve this answer




















        • That's brilliant solution which i highly like .
          – Î±Ô‹É±Ò½Ôƒ αмєяιcαη
          Dec 22 '17 at 10:11














        up vote
        3
        down vote



        accepted










        Perl solution:



        perl -ne 'print if tr/,// > 4'



        • -n reads the file line by line

        • the tr operator returns the number of matches.

        To print the lines with less than 4, just change > to <.






        share|improve this answer




















        • That's brilliant solution which i highly like .
          – Î±Ô‹É±Ò½Ôƒ αмєяιcαη
          Dec 22 '17 at 10:11












        up vote
        3
        down vote



        accepted







        up vote
        3
        down vote



        accepted






        Perl solution:



        perl -ne 'print if tr/,// > 4'



        • -n reads the file line by line

        • the tr operator returns the number of matches.

        To print the lines with less than 4, just change > to <.






        share|improve this answer












        Perl solution:



        perl -ne 'print if tr/,// > 4'



        • -n reads the file line by line

        • the tr operator returns the number of matches.

        To print the lines with less than 4, just change > to <.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 22 '17 at 10:09









        choroba

        24.4k34067




        24.4k34067











        • That's brilliant solution which i highly like .
          – Î±Ô‹É±Ò½Ôƒ αмєяιcαη
          Dec 22 '17 at 10:11
















        • That's brilliant solution which i highly like .
          – Î±Ô‹É±Ò½Ôƒ αмєяιcαη
          Dec 22 '17 at 10:11















        That's brilliant solution which i highly like .
        – Î±Ô‹É±Ò½Ôƒ αмєяιcαη
        Dec 22 '17 at 10:11




        That's brilliant solution which i highly like .
        – Î±Ô‹É±Ò½Ôƒ αмєяιcαη
        Dec 22 '17 at 10:11












        up vote
        1
        down vote













        Using the grep command:



        grep -E '(,.*)5,' myfile


        does the job. Explanation:



        -E: use an Extended Regex...



        '(,.*): ... to find one comma followed by any number of characters, even zero...



        5,': ... and repeat the previous pattern 5 times or more.



        If you want to grep lines with less than 4 commas, replace 5,' with 0,3'.






        share|improve this answer


























          up vote
          1
          down vote













          Using the grep command:



          grep -E '(,.*)5,' myfile


          does the job. Explanation:



          -E: use an Extended Regex...



          '(,.*): ... to find one comma followed by any number of characters, even zero...



          5,': ... and repeat the previous pattern 5 times or more.



          If you want to grep lines with less than 4 commas, replace 5,' with 0,3'.






          share|improve this answer
























            up vote
            1
            down vote










            up vote
            1
            down vote









            Using the grep command:



            grep -E '(,.*)5,' myfile


            does the job. Explanation:



            -E: use an Extended Regex...



            '(,.*): ... to find one comma followed by any number of characters, even zero...



            5,': ... and repeat the previous pattern 5 times or more.



            If you want to grep lines with less than 4 commas, replace 5,' with 0,3'.






            share|improve this answer














            Using the grep command:



            grep -E '(,.*)5,' myfile


            does the job. Explanation:



            -E: use an Extended Regex...



            '(,.*): ... to find one comma followed by any number of characters, even zero...



            5,': ... and repeat the previous pattern 5 times or more.



            If you want to grep lines with less than 4 commas, replace 5,' with 0,3'.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 22 '17 at 10:14

























            answered Dec 22 '17 at 10:08









            dr01

            15.3k114769




            15.3k114769




















                up vote
                1
                down vote













                The awk version:



                awk -F, 'NF > 5' myfile





                share|improve this answer
























                  up vote
                  1
                  down vote













                  The awk version:



                  awk -F, 'NF > 5' myfile





                  share|improve this answer






















                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    The awk version:



                    awk -F, 'NF > 5' myfile





                    share|improve this answer












                    The awk version:



                    awk -F, 'NF > 5' myfile






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 22 '17 at 10:23









                    n.caillou

                    29216




                    29216




















                        up vote
                        -1
                        down vote













                        Achieved result by below one liner




                        l=`awk 'BEGINprint print gsub(",","")' example.txt |sed '/^$/d' |awk '$1 > 4 print NR'`;sed -n ''$l'p' example.txt 



                        output
                        catch,me,then,say,hello,then





                        share|improve this answer
























                          up vote
                          -1
                          down vote













                          Achieved result by below one liner




                          l=`awk 'BEGINprint print gsub(",","")' example.txt |sed '/^$/d' |awk '$1 > 4 print NR'`;sed -n ''$l'p' example.txt 



                          output
                          catch,me,then,say,hello,then





                          share|improve this answer






















                            up vote
                            -1
                            down vote










                            up vote
                            -1
                            down vote









                            Achieved result by below one liner




                            l=`awk 'BEGINprint print gsub(",","")' example.txt |sed '/^$/d' |awk '$1 > 4 print NR'`;sed -n ''$l'p' example.txt 



                            output
                            catch,me,then,say,hello,then





                            share|improve this answer












                            Achieved result by below one liner




                            l=`awk 'BEGINprint print gsub(",","")' example.txt |sed '/^$/d' |awk '$1 > 4 print NR'`;sed -n ''$l'p' example.txt 



                            output
                            catch,me,then,say,hello,then






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 23 '17 at 9:30









                            Praveen Kumar BS

                            1,010128




                            1,010128






















                                 

                                draft saved


                                draft discarded


























                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f412461%2fhow-to-grep-lines-which-have-more-than-specific-number-of-special-characters%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?

                                Displaying single band from multi-band raster using QGIS

                                How many registers does an x86_64 CPU actually have?