Grep Showing Extra Lines












1














So I have a text file:



4556 4618 7843 8732
4532 0861 1932 5122
3478 893* 6788 6312
5440 3173 8207 0451 67886
6011 2966 7184 4668
3678 3905 5323
2389 4387 9336 2783
239 235 453 3458
182 534 654 765
4485 0721 1308 2759
46759 543 2345


I want to grep only the numbers that have 4 digits together, 4 times in a row (seperated by a space).



For example: 4556 4618 7843 8732



I am using: grep -E "([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})" test.txt



Which shows me:



4556 4618 7843 8732
4532 0861 1932 5122
5440 3173 8207 0451 67886
6011 2966 7184 4668
4485 0721 1308 2759


Using this there is an extra line that shouldn't appear, where there is a 5th set of numbers that has 5 digits on the end.



So I used: grep -E "([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})$" test.txt



But this only gave me two results instead of the 4 it should:



4556 4618 7843 8732
4485 0721 1308 2759


Can someone tell me what I'm doing wrong?










share|improve this question






















  • You also appear to be missing 2389 4387 9336 2783 -- are extra spaces the eliminating factor?
    – glenn jackman
    Dec 13 at 22:00


















1














So I have a text file:



4556 4618 7843 8732
4532 0861 1932 5122
3478 893* 6788 6312
5440 3173 8207 0451 67886
6011 2966 7184 4668
3678 3905 5323
2389 4387 9336 2783
239 235 453 3458
182 534 654 765
4485 0721 1308 2759
46759 543 2345


I want to grep only the numbers that have 4 digits together, 4 times in a row (seperated by a space).



For example: 4556 4618 7843 8732



I am using: grep -E "([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})" test.txt



Which shows me:



4556 4618 7843 8732
4532 0861 1932 5122
5440 3173 8207 0451 67886
6011 2966 7184 4668
4485 0721 1308 2759


Using this there is an extra line that shouldn't appear, where there is a 5th set of numbers that has 5 digits on the end.



So I used: grep -E "([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})$" test.txt



But this only gave me two results instead of the 4 it should:



4556 4618 7843 8732
4485 0721 1308 2759


Can someone tell me what I'm doing wrong?










share|improve this question






















  • You also appear to be missing 2389 4387 9336 2783 -- are extra spaces the eliminating factor?
    – glenn jackman
    Dec 13 at 22:00
















1












1








1







So I have a text file:



4556 4618 7843 8732
4532 0861 1932 5122
3478 893* 6788 6312
5440 3173 8207 0451 67886
6011 2966 7184 4668
3678 3905 5323
2389 4387 9336 2783
239 235 453 3458
182 534 654 765
4485 0721 1308 2759
46759 543 2345


I want to grep only the numbers that have 4 digits together, 4 times in a row (seperated by a space).



For example: 4556 4618 7843 8732



I am using: grep -E "([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})" test.txt



Which shows me:



4556 4618 7843 8732
4532 0861 1932 5122
5440 3173 8207 0451 67886
6011 2966 7184 4668
4485 0721 1308 2759


Using this there is an extra line that shouldn't appear, where there is a 5th set of numbers that has 5 digits on the end.



So I used: grep -E "([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})$" test.txt



But this only gave me two results instead of the 4 it should:



4556 4618 7843 8732
4485 0721 1308 2759


Can someone tell me what I'm doing wrong?










share|improve this question













So I have a text file:



4556 4618 7843 8732
4532 0861 1932 5122
3478 893* 6788 6312
5440 3173 8207 0451 67886
6011 2966 7184 4668
3678 3905 5323
2389 4387 9336 2783
239 235 453 3458
182 534 654 765
4485 0721 1308 2759
46759 543 2345


I want to grep only the numbers that have 4 digits together, 4 times in a row (seperated by a space).



For example: 4556 4618 7843 8732



I am using: grep -E "([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})" test.txt



Which shows me:



4556 4618 7843 8732
4532 0861 1932 5122
5440 3173 8207 0451 67886
6011 2966 7184 4668
4485 0721 1308 2759


Using this there is an extra line that shouldn't appear, where there is a 5th set of numbers that has 5 digits on the end.



So I used: grep -E "([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})$" test.txt



But this only gave me two results instead of the 4 it should:



4556 4618 7843 8732
4485 0721 1308 2759


Can someone tell me what I'm doing wrong?







linux grep






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 13 at 18:43









ESuth

82




82












  • You also appear to be missing 2389 4387 9336 2783 -- are extra spaces the eliminating factor?
    – glenn jackman
    Dec 13 at 22:00




















  • You also appear to be missing 2389 4387 9336 2783 -- are extra spaces the eliminating factor?
    – glenn jackman
    Dec 13 at 22:00


















You also appear to be missing 2389 4387 9336 2783 -- are extra spaces the eliminating factor?
– glenn jackman
Dec 13 at 22:00






You also appear to be missing 2389 4387 9336 2783 -- are extra spaces the eliminating factor?
– glenn jackman
Dec 13 at 22:00












2 Answers
2






active

oldest

votes


















3














$ grep -E '^[[:blank:]]*[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}[[:blank:]]*$' file
4556 4618 7843 8732
4532 0861 1932 5122
6011 2966 7184 4668
4485 0721 1308 2759


Your expression matches lines with four or more sets of space-delimited four-digit numbers. The parentheses don't do anything in the expression.



The expression above anchors the pattern to the start and end of the line and only allows spaces or tabs to exist before the first and after the last sets of digits.



As an alternative to using the ^ and $ anchors, you could instead use grep -x:



grep -Ex '[[:blank:]]*[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}[[:blank:]]*'


And shortening this, just like Jeff has shown,



grep -Ex '[[:blank:]]*([0-9]{4} ){3}[0-9]{4}[[:blank:]]*'





share|improve this answer























  • This works great, exactly what I was looking for. I was forgetting to use *
    – ESuth
    Dec 13 at 18:51



















3














You got halfway there with the end-of-line anchor $; you just need to anchor the beginning of the line, too, with ^. Looks like you're OK with a leading space, so allow for that as well, with *:



grep -E "^ *([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})$" test.txt


If it helps to simplify the typing (or understanding) any, you could combine the first three patterns:



grep -E "^ *([[:digit:]]{4} ){3}[[:digit:]]{4}$"


... meaning you want 3 of the quantity (4 digits followed by a space) followed by a space, then 4 digits, then EOL.






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%2f487831%2fgrep-showing-extra-lines%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    $ grep -E '^[[:blank:]]*[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}[[:blank:]]*$' file
    4556 4618 7843 8732
    4532 0861 1932 5122
    6011 2966 7184 4668
    4485 0721 1308 2759


    Your expression matches lines with four or more sets of space-delimited four-digit numbers. The parentheses don't do anything in the expression.



    The expression above anchors the pattern to the start and end of the line and only allows spaces or tabs to exist before the first and after the last sets of digits.



    As an alternative to using the ^ and $ anchors, you could instead use grep -x:



    grep -Ex '[[:blank:]]*[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}[[:blank:]]*'


    And shortening this, just like Jeff has shown,



    grep -Ex '[[:blank:]]*([0-9]{4} ){3}[0-9]{4}[[:blank:]]*'





    share|improve this answer























    • This works great, exactly what I was looking for. I was forgetting to use *
      – ESuth
      Dec 13 at 18:51
















    3














    $ grep -E '^[[:blank:]]*[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}[[:blank:]]*$' file
    4556 4618 7843 8732
    4532 0861 1932 5122
    6011 2966 7184 4668
    4485 0721 1308 2759


    Your expression matches lines with four or more sets of space-delimited four-digit numbers. The parentheses don't do anything in the expression.



    The expression above anchors the pattern to the start and end of the line and only allows spaces or tabs to exist before the first and after the last sets of digits.



    As an alternative to using the ^ and $ anchors, you could instead use grep -x:



    grep -Ex '[[:blank:]]*[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}[[:blank:]]*'


    And shortening this, just like Jeff has shown,



    grep -Ex '[[:blank:]]*([0-9]{4} ){3}[0-9]{4}[[:blank:]]*'





    share|improve this answer























    • This works great, exactly what I was looking for. I was forgetting to use *
      – ESuth
      Dec 13 at 18:51














    3












    3








    3






    $ grep -E '^[[:blank:]]*[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}[[:blank:]]*$' file
    4556 4618 7843 8732
    4532 0861 1932 5122
    6011 2966 7184 4668
    4485 0721 1308 2759


    Your expression matches lines with four or more sets of space-delimited four-digit numbers. The parentheses don't do anything in the expression.



    The expression above anchors the pattern to the start and end of the line and only allows spaces or tabs to exist before the first and after the last sets of digits.



    As an alternative to using the ^ and $ anchors, you could instead use grep -x:



    grep -Ex '[[:blank:]]*[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}[[:blank:]]*'


    And shortening this, just like Jeff has shown,



    grep -Ex '[[:blank:]]*([0-9]{4} ){3}[0-9]{4}[[:blank:]]*'





    share|improve this answer














    $ grep -E '^[[:blank:]]*[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}[[:blank:]]*$' file
    4556 4618 7843 8732
    4532 0861 1932 5122
    6011 2966 7184 4668
    4485 0721 1308 2759


    Your expression matches lines with four or more sets of space-delimited four-digit numbers. The parentheses don't do anything in the expression.



    The expression above anchors the pattern to the start and end of the line and only allows spaces or tabs to exist before the first and after the last sets of digits.



    As an alternative to using the ^ and $ anchors, you could instead use grep -x:



    grep -Ex '[[:blank:]]*[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}[[:blank:]]*'


    And shortening this, just like Jeff has shown,



    grep -Ex '[[:blank:]]*([0-9]{4} ){3}[0-9]{4}[[:blank:]]*'






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 13 at 19:50

























    answered Dec 13 at 18:48









    Kusalananda

    121k16227372




    121k16227372












    • This works great, exactly what I was looking for. I was forgetting to use *
      – ESuth
      Dec 13 at 18:51


















    • This works great, exactly what I was looking for. I was forgetting to use *
      – ESuth
      Dec 13 at 18:51
















    This works great, exactly what I was looking for. I was forgetting to use *
    – ESuth
    Dec 13 at 18:51




    This works great, exactly what I was looking for. I was forgetting to use *
    – ESuth
    Dec 13 at 18:51













    3














    You got halfway there with the end-of-line anchor $; you just need to anchor the beginning of the line, too, with ^. Looks like you're OK with a leading space, so allow for that as well, with *:



    grep -E "^ *([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})$" test.txt


    If it helps to simplify the typing (or understanding) any, you could combine the first three patterns:



    grep -E "^ *([[:digit:]]{4} ){3}[[:digit:]]{4}$"


    ... meaning you want 3 of the quantity (4 digits followed by a space) followed by a space, then 4 digits, then EOL.






    share|improve this answer


























      3














      You got halfway there with the end-of-line anchor $; you just need to anchor the beginning of the line, too, with ^. Looks like you're OK with a leading space, so allow for that as well, with *:



      grep -E "^ *([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})$" test.txt


      If it helps to simplify the typing (or understanding) any, you could combine the first three patterns:



      grep -E "^ *([[:digit:]]{4} ){3}[[:digit:]]{4}$"


      ... meaning you want 3 of the quantity (4 digits followed by a space) followed by a space, then 4 digits, then EOL.






      share|improve this answer
























        3












        3








        3






        You got halfway there with the end-of-line anchor $; you just need to anchor the beginning of the line, too, with ^. Looks like you're OK with a leading space, so allow for that as well, with *:



        grep -E "^ *([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})$" test.txt


        If it helps to simplify the typing (or understanding) any, you could combine the first three patterns:



        grep -E "^ *([[:digit:]]{4} ){3}[[:digit:]]{4}$"


        ... meaning you want 3 of the quantity (4 digits followed by a space) followed by a space, then 4 digits, then EOL.






        share|improve this answer












        You got halfway there with the end-of-line anchor $; you just need to anchor the beginning of the line, too, with ^. Looks like you're OK with a leading space, so allow for that as well, with *:



        grep -E "^ *([0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4})$" test.txt


        If it helps to simplify the typing (or understanding) any, you could combine the first three patterns:



        grep -E "^ *([[:digit:]]{4} ){3}[[:digit:]]{4}$"


        ... meaning you want 3 of the quantity (4 digits followed by a space) followed by a space, then 4 digits, then EOL.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 13 at 18:47









        Jeff Schaller

        38.3k1053125




        38.3k1053125






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f487831%2fgrep-showing-extra-lines%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

            Сан-Квентин

            8-я гвардейская общевойсковая армия

            Алькесар