What is the correct way to format this grep?












0















I am having trouble figuring out where to place the in this command.



grep "^D.*(A1|A2|A3)$" input.txt > output.txt


I'm searching for each line that starts with a D and ends with A1, A2, or A3, which is at the end of the line.










share|improve this question

























  • Try the -E option.

    – Gunnar Hjalmarsson
    Jan 25 at 23:23






  • 1





    For this particular case, you could use a simple character range ^D.*A[1-3]$

    – steeldriver
    Jan 26 at 2:09
















0















I am having trouble figuring out where to place the in this command.



grep "^D.*(A1|A2|A3)$" input.txt > output.txt


I'm searching for each line that starts with a D and ends with A1, A2, or A3, which is at the end of the line.










share|improve this question

























  • Try the -E option.

    – Gunnar Hjalmarsson
    Jan 25 at 23:23






  • 1





    For this particular case, you could use a simple character range ^D.*A[1-3]$

    – steeldriver
    Jan 26 at 2:09














0












0








0








I am having trouble figuring out where to place the in this command.



grep "^D.*(A1|A2|A3)$" input.txt > output.txt


I'm searching for each line that starts with a D and ends with A1, A2, or A3, which is at the end of the line.










share|improve this question
















I am having trouble figuring out where to place the in this command.



grep "^D.*(A1|A2|A3)$" input.txt > output.txt


I'm searching for each line that starts with a D and ends with A1, A2, or A3, which is at the end of the line.







command-line grep regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 25 at 23:39









wjandrea

9,13942363




9,13942363










asked Jan 25 at 23:00









Sim GrocSim Groc

31




31













  • Try the -E option.

    – Gunnar Hjalmarsson
    Jan 25 at 23:23






  • 1





    For this particular case, you could use a simple character range ^D.*A[1-3]$

    – steeldriver
    Jan 26 at 2:09



















  • Try the -E option.

    – Gunnar Hjalmarsson
    Jan 25 at 23:23






  • 1





    For this particular case, you could use a simple character range ^D.*A[1-3]$

    – steeldriver
    Jan 26 at 2:09

















Try the -E option.

– Gunnar Hjalmarsson
Jan 25 at 23:23





Try the -E option.

– Gunnar Hjalmarsson
Jan 25 at 23:23




1




1





For this particular case, you could use a simple character range ^D.*A[1-3]$

– steeldriver
Jan 26 at 2:09





For this particular case, you could use a simple character range ^D.*A[1-3]$

– steeldriver
Jan 26 at 2:09










3 Answers
3






active

oldest

votes


















3














You just needed to escape the or bars - you were almost there:



grep "^D.*(A1|A2|A3)$"


Note you can also use egrep instead of all the escapes:



egrep "^D.*(A1|A2|A3)$"





share|improve this answer































    5














    You need to escape the pipes (|).



    $ grep "^D.*(A1|A2|A3)$" <(printf 'D%sn' A1 A2 A3)
    DA1
    DA2
    DA3


    Or use option -E for extended regex, then you don't need to escape anything.



    $ grep -E "^D.*(A1|A2|A3)$" <(printf 'D%sn' A1 A2 A3)
    DA1
    DA2
    DA3





    share|improve this answer
























    • We can use also -P instead of -E and this is really useful in some cases, see: Grep -E, Sed -E - low performance, but why?

      – pa4080
      Jan 26 at 14:36





















    0














    Here is sed implementation, that uses the combination of the option -n and the command p:





    sed -rn '/^D.*(A1|A2|A3)$/p' in-file




    sed -n '/^D.*(A1|A2|A3)$/p' in-file




    sed -n '/^D.*A[1-3]$/p' in-file



    • option -r, --regexp-extended: use extended regular expressions in the script.


    • option -n, --quiet, --silent: suppress automatic printing of pattern space.


    • command p: print the current pattern space.







    share|improve this answer

























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "89"
      };
      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: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      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%2faskubuntu.com%2fquestions%2f1112944%2fwhat-is-the-correct-way-to-format-this-grep%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      You just needed to escape the or bars - you were almost there:



      grep "^D.*(A1|A2|A3)$"


      Note you can also use egrep instead of all the escapes:



      egrep "^D.*(A1|A2|A3)$"





      share|improve this answer




























        3














        You just needed to escape the or bars - you were almost there:



        grep "^D.*(A1|A2|A3)$"


        Note you can also use egrep instead of all the escapes:



        egrep "^D.*(A1|A2|A3)$"





        share|improve this answer


























          3












          3








          3







          You just needed to escape the or bars - you were almost there:



          grep "^D.*(A1|A2|A3)$"


          Note you can also use egrep instead of all the escapes:



          egrep "^D.*(A1|A2|A3)$"





          share|improve this answer













          You just needed to escape the or bars - you were almost there:



          grep "^D.*(A1|A2|A3)$"


          Note you can also use egrep instead of all the escapes:



          egrep "^D.*(A1|A2|A3)$"






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 25 at 23:36









          Eric MintzEric Mintz

          662312




          662312

























              5














              You need to escape the pipes (|).



              $ grep "^D.*(A1|A2|A3)$" <(printf 'D%sn' A1 A2 A3)
              DA1
              DA2
              DA3


              Or use option -E for extended regex, then you don't need to escape anything.



              $ grep -E "^D.*(A1|A2|A3)$" <(printf 'D%sn' A1 A2 A3)
              DA1
              DA2
              DA3





              share|improve this answer
























              • We can use also -P instead of -E and this is really useful in some cases, see: Grep -E, Sed -E - low performance, but why?

                – pa4080
                Jan 26 at 14:36


















              5














              You need to escape the pipes (|).



              $ grep "^D.*(A1|A2|A3)$" <(printf 'D%sn' A1 A2 A3)
              DA1
              DA2
              DA3


              Or use option -E for extended regex, then you don't need to escape anything.



              $ grep -E "^D.*(A1|A2|A3)$" <(printf 'D%sn' A1 A2 A3)
              DA1
              DA2
              DA3





              share|improve this answer
























              • We can use also -P instead of -E and this is really useful in some cases, see: Grep -E, Sed -E - low performance, but why?

                – pa4080
                Jan 26 at 14:36
















              5












              5








              5







              You need to escape the pipes (|).



              $ grep "^D.*(A1|A2|A3)$" <(printf 'D%sn' A1 A2 A3)
              DA1
              DA2
              DA3


              Or use option -E for extended regex, then you don't need to escape anything.



              $ grep -E "^D.*(A1|A2|A3)$" <(printf 'D%sn' A1 A2 A3)
              DA1
              DA2
              DA3





              share|improve this answer













              You need to escape the pipes (|).



              $ grep "^D.*(A1|A2|A3)$" <(printf 'D%sn' A1 A2 A3)
              DA1
              DA2
              DA3


              Or use option -E for extended regex, then you don't need to escape anything.



              $ grep -E "^D.*(A1|A2|A3)$" <(printf 'D%sn' A1 A2 A3)
              DA1
              DA2
              DA3






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 25 at 23:33









              wjandreawjandrea

              9,13942363




              9,13942363













              • We can use also -P instead of -E and this is really useful in some cases, see: Grep -E, Sed -E - low performance, but why?

                – pa4080
                Jan 26 at 14:36





















              • We can use also -P instead of -E and this is really useful in some cases, see: Grep -E, Sed -E - low performance, but why?

                – pa4080
                Jan 26 at 14:36



















              We can use also -P instead of -E and this is really useful in some cases, see: Grep -E, Sed -E - low performance, but why?

              – pa4080
              Jan 26 at 14:36







              We can use also -P instead of -E and this is really useful in some cases, see: Grep -E, Sed -E - low performance, but why?

              – pa4080
              Jan 26 at 14:36













              0














              Here is sed implementation, that uses the combination of the option -n and the command p:





              sed -rn '/^D.*(A1|A2|A3)$/p' in-file




              sed -n '/^D.*(A1|A2|A3)$/p' in-file




              sed -n '/^D.*A[1-3]$/p' in-file



              • option -r, --regexp-extended: use extended regular expressions in the script.


              • option -n, --quiet, --silent: suppress automatic printing of pattern space.


              • command p: print the current pattern space.







              share|improve this answer






























                0














                Here is sed implementation, that uses the combination of the option -n and the command p:





                sed -rn '/^D.*(A1|A2|A3)$/p' in-file




                sed -n '/^D.*(A1|A2|A3)$/p' in-file




                sed -n '/^D.*A[1-3]$/p' in-file



                • option -r, --regexp-extended: use extended regular expressions in the script.


                • option -n, --quiet, --silent: suppress automatic printing of pattern space.


                • command p: print the current pattern space.







                share|improve this answer




























                  0












                  0








                  0







                  Here is sed implementation, that uses the combination of the option -n and the command p:





                  sed -rn '/^D.*(A1|A2|A3)$/p' in-file




                  sed -n '/^D.*(A1|A2|A3)$/p' in-file




                  sed -n '/^D.*A[1-3]$/p' in-file



                  • option -r, --regexp-extended: use extended regular expressions in the script.


                  • option -n, --quiet, --silent: suppress automatic printing of pattern space.


                  • command p: print the current pattern space.







                  share|improve this answer















                  Here is sed implementation, that uses the combination of the option -n and the command p:





                  sed -rn '/^D.*(A1|A2|A3)$/p' in-file




                  sed -n '/^D.*(A1|A2|A3)$/p' in-file




                  sed -n '/^D.*A[1-3]$/p' in-file



                  • option -r, --regexp-extended: use extended regular expressions in the script.


                  • option -n, --quiet, --silent: suppress automatic printing of pattern space.


                  • command p: print the current pattern space.








                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 26 at 14:26

























                  answered Jan 26 at 13:37









                  pa4080pa4080

                  14.2k52668




                  14.2k52668






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Ask Ubuntu!


                      • 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%2faskubuntu.com%2fquestions%2f1112944%2fwhat-is-the-correct-way-to-format-this-grep%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

                      How to make a Squid Proxy server?

                      Is this a new Fibonacci Identity?

                      19世紀