Print only the lines that with all digits except the last one or the last two characters or the first or...











up vote
0
down vote

favorite












I have a big text file has lines with all numbers and with all letters and characters and also has lines mixed with numbers , letters and characters i want to print only the lines that have all numbers except the last or the last two characters.





  1. Print the line that start with number but ends with any character at the last or the last two characters that is not a digit.
    For example



    1234567a
    2245678902da
    A1234566d
    12345678abc


    The output have to be



    1234567a
    22345678902da



  2. Print the lines that have all their characters are digits except the first or the first and second characters to be not a digit.
    For example



    A1234
    Ab1234
    1a1234
    Abc1234


    The output have to be



    A1234
    Ab1234



Thanks










share|improve this question









New contributor




Crun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote

    favorite












    I have a big text file has lines with all numbers and with all letters and characters and also has lines mixed with numbers , letters and characters i want to print only the lines that have all numbers except the last or the last two characters.





    1. Print the line that start with number but ends with any character at the last or the last two characters that is not a digit.
      For example



      1234567a
      2245678902da
      A1234566d
      12345678abc


      The output have to be



      1234567a
      22345678902da



    2. Print the lines that have all their characters are digits except the first or the first and second characters to be not a digit.
      For example



      A1234
      Ab1234
      1a1234
      Abc1234


      The output have to be



      A1234
      Ab1234



    Thanks










    share|improve this question









    New contributor




    Crun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a big text file has lines with all numbers and with all letters and characters and also has lines mixed with numbers , letters and characters i want to print only the lines that have all numbers except the last or the last two characters.





      1. Print the line that start with number but ends with any character at the last or the last two characters that is not a digit.
        For example



        1234567a
        2245678902da
        A1234566d
        12345678abc


        The output have to be



        1234567a
        22345678902da



      2. Print the lines that have all their characters are digits except the first or the first and second characters to be not a digit.
        For example



        A1234
        Ab1234
        1a1234
        Abc1234


        The output have to be



        A1234
        Ab1234



      Thanks










      share|improve this question









      New contributor




      Crun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I have a big text file has lines with all numbers and with all letters and characters and also has lines mixed with numbers , letters and characters i want to print only the lines that have all numbers except the last or the last two characters.





      1. Print the line that start with number but ends with any character at the last or the last two characters that is not a digit.
        For example



        1234567a
        2245678902da
        A1234566d
        12345678abc


        The output have to be



        1234567a
        22345678902da



      2. Print the lines that have all their characters are digits except the first or the first and second characters to be not a digit.
        For example



        A1234
        Ab1234
        1a1234
        Abc1234


        The output have to be



        A1234
        Ab1234



      Thanks







      sed command-line






      share|improve this question









      New contributor




      Crun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Crun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited yesterday









      Kusalananda

      120k16225369




      120k16225369






      New contributor




      Crun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked yesterday









      Crun

      12




      12




      New contributor




      Crun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Crun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Crun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote













          $ grep -Ex '[[:digit:]]+(.|[^[:digit:]]{2})' file1
          1234567a
          2245678902da


          The extended regular expression [[:digit:]]+(.|[^[:digit:]]{2}) will match one or more digits followed by either an unspecified character or two non-digits (this is a literal interpretation of your specification "start with number but ends with any character at the last or the last two characters that is not a digit"). The -x option to grep ensures that the match will be across full lines.



          Note that this literal interpretation of what you specified also matches lines that contain only digits.



          With



          $ grep -Ex '[^[:digit:]]{1,2}[[:digit:]]+' file2
          A1234
          Ab1234


          we match lines that start with one or two non-digits, and then contains one or more digits until the end of the line.





          For a visual representation of the two regular expressions (and also at the same time showing how to do it with sed):



          The first:



          $ sed -nE 's/^([[:digit:]]+)(.|[^[:digit:]]{2})$/(1)(2)/p' file1
          (234567)(a)
          (2245678902)(da)


          The second:



          $ sed -nE 's/^([^[:digit:]]{1,2})([[:digit:]]+)$/(1)(2)/p' file2
          (A)(1234)
          (Ab)(1234)


          Remove all parentheses from the sed command to get the sed solution.






          share|improve this answer






























            up vote
            0
            down vote













            Slightly different interpretation. I read this as the last 1-2 characters cannot be digits so I get



            grep -E '^[[:digit:]]+[^[:digit:]]{1,2}$' file
            1234567a
            2245678902da





            share|improve this answer





















            • Hello all How to except the first character or or the first and second characters from being digits to print these lines ?
              – Crun
              yesterday










            • @Crun: lease don't expand your question in a comment; edit the question. And, why not try to apply the lessons learned in here to provide your own solution?
              – RudiC
              yesterday












            • @Kusalananda your answer is not right cos it prints the line if it has for example 12345c3 but the answer required to be the last or the last two characters not to be digits
              – Crun
              yesterday










            • @Crun When I answered, your question specified that the last two characters were allowed to be anything. I interpreted "anything" as anything, including digits. I see that you have updated the question and will update my answer in a while. Also, comments on my answer should go beneath my answer.
              – Kusalananda
              yesterday












            • @Doug O’Neal your answer is ok .. thanks. Could you please answer the item #2 in my my question above?
              – Crun
              23 hours ago











            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: 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
            });


            }
            });






            Crun is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f489181%2fprint-only-the-lines-that-with-all-digits-except-the-last-one-or-the-last-two-ch%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








            up vote
            3
            down vote













            $ grep -Ex '[[:digit:]]+(.|[^[:digit:]]{2})' file1
            1234567a
            2245678902da


            The extended regular expression [[:digit:]]+(.|[^[:digit:]]{2}) will match one or more digits followed by either an unspecified character or two non-digits (this is a literal interpretation of your specification "start with number but ends with any character at the last or the last two characters that is not a digit"). The -x option to grep ensures that the match will be across full lines.



            Note that this literal interpretation of what you specified also matches lines that contain only digits.



            With



            $ grep -Ex '[^[:digit:]]{1,2}[[:digit:]]+' file2
            A1234
            Ab1234


            we match lines that start with one or two non-digits, and then contains one or more digits until the end of the line.





            For a visual representation of the two regular expressions (and also at the same time showing how to do it with sed):



            The first:



            $ sed -nE 's/^([[:digit:]]+)(.|[^[:digit:]]{2})$/(1)(2)/p' file1
            (234567)(a)
            (2245678902)(da)


            The second:



            $ sed -nE 's/^([^[:digit:]]{1,2})([[:digit:]]+)$/(1)(2)/p' file2
            (A)(1234)
            (Ab)(1234)


            Remove all parentheses from the sed command to get the sed solution.






            share|improve this answer



























              up vote
              3
              down vote













              $ grep -Ex '[[:digit:]]+(.|[^[:digit:]]{2})' file1
              1234567a
              2245678902da


              The extended regular expression [[:digit:]]+(.|[^[:digit:]]{2}) will match one or more digits followed by either an unspecified character or two non-digits (this is a literal interpretation of your specification "start with number but ends with any character at the last or the last two characters that is not a digit"). The -x option to grep ensures that the match will be across full lines.



              Note that this literal interpretation of what you specified also matches lines that contain only digits.



              With



              $ grep -Ex '[^[:digit:]]{1,2}[[:digit:]]+' file2
              A1234
              Ab1234


              we match lines that start with one or two non-digits, and then contains one or more digits until the end of the line.





              For a visual representation of the two regular expressions (and also at the same time showing how to do it with sed):



              The first:



              $ sed -nE 's/^([[:digit:]]+)(.|[^[:digit:]]{2})$/(1)(2)/p' file1
              (234567)(a)
              (2245678902)(da)


              The second:



              $ sed -nE 's/^([^[:digit:]]{1,2})([[:digit:]]+)$/(1)(2)/p' file2
              (A)(1234)
              (Ab)(1234)


              Remove all parentheses from the sed command to get the sed solution.






              share|improve this answer

























                up vote
                3
                down vote










                up vote
                3
                down vote









                $ grep -Ex '[[:digit:]]+(.|[^[:digit:]]{2})' file1
                1234567a
                2245678902da


                The extended regular expression [[:digit:]]+(.|[^[:digit:]]{2}) will match one or more digits followed by either an unspecified character or two non-digits (this is a literal interpretation of your specification "start with number but ends with any character at the last or the last two characters that is not a digit"). The -x option to grep ensures that the match will be across full lines.



                Note that this literal interpretation of what you specified also matches lines that contain only digits.



                With



                $ grep -Ex '[^[:digit:]]{1,2}[[:digit:]]+' file2
                A1234
                Ab1234


                we match lines that start with one or two non-digits, and then contains one or more digits until the end of the line.





                For a visual representation of the two regular expressions (and also at the same time showing how to do it with sed):



                The first:



                $ sed -nE 's/^([[:digit:]]+)(.|[^[:digit:]]{2})$/(1)(2)/p' file1
                (234567)(a)
                (2245678902)(da)


                The second:



                $ sed -nE 's/^([^[:digit:]]{1,2})([[:digit:]]+)$/(1)(2)/p' file2
                (A)(1234)
                (Ab)(1234)


                Remove all parentheses from the sed command to get the sed solution.






                share|improve this answer














                $ grep -Ex '[[:digit:]]+(.|[^[:digit:]]{2})' file1
                1234567a
                2245678902da


                The extended regular expression [[:digit:]]+(.|[^[:digit:]]{2}) will match one or more digits followed by either an unspecified character or two non-digits (this is a literal interpretation of your specification "start with number but ends with any character at the last or the last two characters that is not a digit"). The -x option to grep ensures that the match will be across full lines.



                Note that this literal interpretation of what you specified also matches lines that contain only digits.



                With



                $ grep -Ex '[^[:digit:]]{1,2}[[:digit:]]+' file2
                A1234
                Ab1234


                we match lines that start with one or two non-digits, and then contains one or more digits until the end of the line.





                For a visual representation of the two regular expressions (and also at the same time showing how to do it with sed):



                The first:



                $ sed -nE 's/^([[:digit:]]+)(.|[^[:digit:]]{2})$/(1)(2)/p' file1
                (234567)(a)
                (2245678902)(da)


                The second:



                $ sed -nE 's/^([^[:digit:]]{1,2})([[:digit:]]+)$/(1)(2)/p' file2
                (A)(1234)
                (Ab)(1234)


                Remove all parentheses from the sed command to get the sed solution.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited yesterday

























                answered yesterday









                Kusalananda

                120k16225369




                120k16225369
























                    up vote
                    0
                    down vote













                    Slightly different interpretation. I read this as the last 1-2 characters cannot be digits so I get



                    grep -E '^[[:digit:]]+[^[:digit:]]{1,2}$' file
                    1234567a
                    2245678902da





                    share|improve this answer





















                    • Hello all How to except the first character or or the first and second characters from being digits to print these lines ?
                      – Crun
                      yesterday










                    • @Crun: lease don't expand your question in a comment; edit the question. And, why not try to apply the lessons learned in here to provide your own solution?
                      – RudiC
                      yesterday












                    • @Kusalananda your answer is not right cos it prints the line if it has for example 12345c3 but the answer required to be the last or the last two characters not to be digits
                      – Crun
                      yesterday










                    • @Crun When I answered, your question specified that the last two characters were allowed to be anything. I interpreted "anything" as anything, including digits. I see that you have updated the question and will update my answer in a while. Also, comments on my answer should go beneath my answer.
                      – Kusalananda
                      yesterday












                    • @Doug O’Neal your answer is ok .. thanks. Could you please answer the item #2 in my my question above?
                      – Crun
                      23 hours ago















                    up vote
                    0
                    down vote













                    Slightly different interpretation. I read this as the last 1-2 characters cannot be digits so I get



                    grep -E '^[[:digit:]]+[^[:digit:]]{1,2}$' file
                    1234567a
                    2245678902da





                    share|improve this answer





















                    • Hello all How to except the first character or or the first and second characters from being digits to print these lines ?
                      – Crun
                      yesterday










                    • @Crun: lease don't expand your question in a comment; edit the question. And, why not try to apply the lessons learned in here to provide your own solution?
                      – RudiC
                      yesterday












                    • @Kusalananda your answer is not right cos it prints the line if it has for example 12345c3 but the answer required to be the last or the last two characters not to be digits
                      – Crun
                      yesterday










                    • @Crun When I answered, your question specified that the last two characters were allowed to be anything. I interpreted "anything" as anything, including digits. I see that you have updated the question and will update my answer in a while. Also, comments on my answer should go beneath my answer.
                      – Kusalananda
                      yesterday












                    • @Doug O’Neal your answer is ok .. thanks. Could you please answer the item #2 in my my question above?
                      – Crun
                      23 hours ago













                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    Slightly different interpretation. I read this as the last 1-2 characters cannot be digits so I get



                    grep -E '^[[:digit:]]+[^[:digit:]]{1,2}$' file
                    1234567a
                    2245678902da





                    share|improve this answer












                    Slightly different interpretation. I read this as the last 1-2 characters cannot be digits so I get



                    grep -E '^[[:digit:]]+[^[:digit:]]{1,2}$' file
                    1234567a
                    2245678902da






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered yesterday









                    Doug O'Neal

                    2,8321817




                    2,8321817












                    • Hello all How to except the first character or or the first and second characters from being digits to print these lines ?
                      – Crun
                      yesterday










                    • @Crun: lease don't expand your question in a comment; edit the question. And, why not try to apply the lessons learned in here to provide your own solution?
                      – RudiC
                      yesterday












                    • @Kusalananda your answer is not right cos it prints the line if it has for example 12345c3 but the answer required to be the last or the last two characters not to be digits
                      – Crun
                      yesterday










                    • @Crun When I answered, your question specified that the last two characters were allowed to be anything. I interpreted "anything" as anything, including digits. I see that you have updated the question and will update my answer in a while. Also, comments on my answer should go beneath my answer.
                      – Kusalananda
                      yesterday












                    • @Doug O’Neal your answer is ok .. thanks. Could you please answer the item #2 in my my question above?
                      – Crun
                      23 hours ago


















                    • Hello all How to except the first character or or the first and second characters from being digits to print these lines ?
                      – Crun
                      yesterday










                    • @Crun: lease don't expand your question in a comment; edit the question. And, why not try to apply the lessons learned in here to provide your own solution?
                      – RudiC
                      yesterday












                    • @Kusalananda your answer is not right cos it prints the line if it has for example 12345c3 but the answer required to be the last or the last two characters not to be digits
                      – Crun
                      yesterday










                    • @Crun When I answered, your question specified that the last two characters were allowed to be anything. I interpreted "anything" as anything, including digits. I see that you have updated the question and will update my answer in a while. Also, comments on my answer should go beneath my answer.
                      – Kusalananda
                      yesterday












                    • @Doug O’Neal your answer is ok .. thanks. Could you please answer the item #2 in my my question above?
                      – Crun
                      23 hours ago
















                    Hello all How to except the first character or or the first and second characters from being digits to print these lines ?
                    – Crun
                    yesterday




                    Hello all How to except the first character or or the first and second characters from being digits to print these lines ?
                    – Crun
                    yesterday












                    @Crun: lease don't expand your question in a comment; edit the question. And, why not try to apply the lessons learned in here to provide your own solution?
                    – RudiC
                    yesterday






                    @Crun: lease don't expand your question in a comment; edit the question. And, why not try to apply the lessons learned in here to provide your own solution?
                    – RudiC
                    yesterday














                    @Kusalananda your answer is not right cos it prints the line if it has for example 12345c3 but the answer required to be the last or the last two characters not to be digits
                    – Crun
                    yesterday




                    @Kusalananda your answer is not right cos it prints the line if it has for example 12345c3 but the answer required to be the last or the last two characters not to be digits
                    – Crun
                    yesterday












                    @Crun When I answered, your question specified that the last two characters were allowed to be anything. I interpreted "anything" as anything, including digits. I see that you have updated the question and will update my answer in a while. Also, comments on my answer should go beneath my answer.
                    – Kusalananda
                    yesterday






                    @Crun When I answered, your question specified that the last two characters were allowed to be anything. I interpreted "anything" as anything, including digits. I see that you have updated the question and will update my answer in a while. Also, comments on my answer should go beneath my answer.
                    – Kusalananda
                    yesterday














                    @Doug O’Neal your answer is ok .. thanks. Could you please answer the item #2 in my my question above?
                    – Crun
                    23 hours ago




                    @Doug O’Neal your answer is ok .. thanks. Could you please answer the item #2 in my my question above?
                    – Crun
                    23 hours ago










                    Crun is a new contributor. Be nice, and check out our Code of Conduct.










                    draft saved

                    draft discarded


















                    Crun is a new contributor. Be nice, and check out our Code of Conduct.













                    Crun is a new contributor. Be nice, and check out our Code of Conduct.












                    Crun is a new contributor. Be nice, and check out our Code of Conduct.
















                    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%2f489181%2fprint-only-the-lines-that-with-all-digits-except-the-last-one-or-the-last-two-ch%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

                    サソリ

                    広島県道265号伴広島線

                    Setup Asymptote in Texstudio