print entire line after an awk and grep

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












0















I need to search multiple patterns on the third column of a file, and then print the whole line.



I am using this one below but how do I get it to print the whole line where there is a match?



awk 'print $3' file | egrep -w "S|M|D"









share|improve this question






















  • please click edit and add few lines of input and expected output - some line should match and others shouldn't... this will help to add clarity to question as well as act as test data for those who wish to answer your question

    – Sundeep
    Jan 7 at 9:23
















0















I need to search multiple patterns on the third column of a file, and then print the whole line.



I am using this one below but how do I get it to print the whole line where there is a match?



awk 'print $3' file | egrep -w "S|M|D"









share|improve this question






















  • please click edit and add few lines of input and expected output - some line should match and others shouldn't... this will help to add clarity to question as well as act as test data for those who wish to answer your question

    – Sundeep
    Jan 7 at 9:23














0












0








0








I need to search multiple patterns on the third column of a file, and then print the whole line.



I am using this one below but how do I get it to print the whole line where there is a match?



awk 'print $3' file | egrep -w "S|M|D"









share|improve this question














I need to search multiple patterns on the third column of a file, and then print the whole line.



I am using this one below but how do I get it to print the whole line where there is a match?



awk 'print $3' file | egrep -w "S|M|D"






shell-script awk scripting






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 7 at 9:17









hayabusa99hayabusa99

31




31












  • please click edit and add few lines of input and expected output - some line should match and others shouldn't... this will help to add clarity to question as well as act as test data for those who wish to answer your question

    – Sundeep
    Jan 7 at 9:23


















  • please click edit and add few lines of input and expected output - some line should match and others shouldn't... this will help to add clarity to question as well as act as test data for those who wish to answer your question

    – Sundeep
    Jan 7 at 9:23

















please click edit and add few lines of input and expected output - some line should match and others shouldn't... this will help to add clarity to question as well as act as test data for those who wish to answer your question

– Sundeep
Jan 7 at 9:23






please click edit and add few lines of input and expected output - some line should match and others shouldn't... this will help to add clarity to question as well as act as test data for those who wish to answer your question

– Sundeep
Jan 7 at 9:23











4 Answers
4






active

oldest

votes


















3














I think your requirement just needs awk and not a combination with grep. If you are looking to print the whole line where the third column matches any of those letters, you need to do



awk '$3 ~ /^(S|M|D)$/' file





share|improve this answer


















  • 2





    since it is single character, you can also use [SMD]

    – Sundeep
    Jan 7 at 9:28











  • is this going to search for the exact match? like grep -w does?

    – hayabusa99
    Jan 7 at 9:33











  • It look like it works. Thanks!

    – hayabusa99
    Jan 7 at 9:34


















1














To extract the lines whose 3rd whitespace-delimited field is exactly S, M or D, use one of



awk '$3 ~ /^[SMD]$/' file


or, using string matching rather than regular expression matching,



awk '$3 == "S" || $3 == "M" || $3 == "D"' file


A condition without a corresponding block will act as if its block simply was print .






share|improve this answer






























    0














    awk '$3 ~ /^S/||/^M/||/^D/print $0' filename





    share|improve this answer























    • The way this parses out will match column 3 starting with an S, or column 1 starting with an M or D -- it misses column 3 M/D and it incorrectly includes the column 1 matches.

      – Jeff Schaller
      Jan 9 at 19:52


















    0














    Same has been achieved in python and it worked fine



    Code



    #!/usr/bin/python
    import subprocess
    import re
    h=re.compile(r'^[SMD]')
    o=open('l.txt','r')
    for i in o:
    j=i.split(':')
    if re.search(h,j[2]):
    print i.strip()





    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%2f492952%2fprint-entire-line-after-an-awk-and-grep%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      I think your requirement just needs awk and not a combination with grep. If you are looking to print the whole line where the third column matches any of those letters, you need to do



      awk '$3 ~ /^(S|M|D)$/' file





      share|improve this answer


















      • 2





        since it is single character, you can also use [SMD]

        – Sundeep
        Jan 7 at 9:28











      • is this going to search for the exact match? like grep -w does?

        – hayabusa99
        Jan 7 at 9:33











      • It look like it works. Thanks!

        – hayabusa99
        Jan 7 at 9:34















      3














      I think your requirement just needs awk and not a combination with grep. If you are looking to print the whole line where the third column matches any of those letters, you need to do



      awk '$3 ~ /^(S|M|D)$/' file





      share|improve this answer


















      • 2





        since it is single character, you can also use [SMD]

        – Sundeep
        Jan 7 at 9:28











      • is this going to search for the exact match? like grep -w does?

        – hayabusa99
        Jan 7 at 9:33











      • It look like it works. Thanks!

        – hayabusa99
        Jan 7 at 9:34













      3












      3








      3







      I think your requirement just needs awk and not a combination with grep. If you are looking to print the whole line where the third column matches any of those letters, you need to do



      awk '$3 ~ /^(S|M|D)$/' file





      share|improve this answer













      I think your requirement just needs awk and not a combination with grep. If you are looking to print the whole line where the third column matches any of those letters, you need to do



      awk '$3 ~ /^(S|M|D)$/' file






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jan 7 at 9:27









      InianInian

      3,945824




      3,945824







      • 2





        since it is single character, you can also use [SMD]

        – Sundeep
        Jan 7 at 9:28











      • is this going to search for the exact match? like grep -w does?

        – hayabusa99
        Jan 7 at 9:33











      • It look like it works. Thanks!

        – hayabusa99
        Jan 7 at 9:34












      • 2





        since it is single character, you can also use [SMD]

        – Sundeep
        Jan 7 at 9:28











      • is this going to search for the exact match? like grep -w does?

        – hayabusa99
        Jan 7 at 9:33











      • It look like it works. Thanks!

        – hayabusa99
        Jan 7 at 9:34







      2




      2





      since it is single character, you can also use [SMD]

      – Sundeep
      Jan 7 at 9:28





      since it is single character, you can also use [SMD]

      – Sundeep
      Jan 7 at 9:28













      is this going to search for the exact match? like grep -w does?

      – hayabusa99
      Jan 7 at 9:33





      is this going to search for the exact match? like grep -w does?

      – hayabusa99
      Jan 7 at 9:33













      It look like it works. Thanks!

      – hayabusa99
      Jan 7 at 9:34





      It look like it works. Thanks!

      – hayabusa99
      Jan 7 at 9:34













      1














      To extract the lines whose 3rd whitespace-delimited field is exactly S, M or D, use one of



      awk '$3 ~ /^[SMD]$/' file


      or, using string matching rather than regular expression matching,



      awk '$3 == "S" || $3 == "M" || $3 == "D"' file


      A condition without a corresponding block will act as if its block simply was print .






      share|improve this answer



























        1














        To extract the lines whose 3rd whitespace-delimited field is exactly S, M or D, use one of



        awk '$3 ~ /^[SMD]$/' file


        or, using string matching rather than regular expression matching,



        awk '$3 == "S" || $3 == "M" || $3 == "D"' file


        A condition without a corresponding block will act as if its block simply was print .






        share|improve this answer

























          1












          1








          1







          To extract the lines whose 3rd whitespace-delimited field is exactly S, M or D, use one of



          awk '$3 ~ /^[SMD]$/' file


          or, using string matching rather than regular expression matching,



          awk '$3 == "S" || $3 == "M" || $3 == "D"' file


          A condition without a corresponding block will act as if its block simply was print .






          share|improve this answer













          To extract the lines whose 3rd whitespace-delimited field is exactly S, M or D, use one of



          awk '$3 ~ /^[SMD]$/' file


          or, using string matching rather than regular expression matching,



          awk '$3 == "S" || $3 == "M" || $3 == "D"' file


          A condition without a corresponding block will act as if its block simply was print .







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 7 at 9:37









          KusalanandaKusalananda

          125k16236389




          125k16236389





















              0














              awk '$3 ~ /^S/||/^M/||/^D/print $0' filename





              share|improve this answer























              • The way this parses out will match column 3 starting with an S, or column 1 starting with an M or D -- it misses column 3 M/D and it incorrectly includes the column 1 matches.

                – Jeff Schaller
                Jan 9 at 19:52















              0














              awk '$3 ~ /^S/||/^M/||/^D/print $0' filename





              share|improve this answer























              • The way this parses out will match column 3 starting with an S, or column 1 starting with an M or D -- it misses column 3 M/D and it incorrectly includes the column 1 matches.

                – Jeff Schaller
                Jan 9 at 19:52













              0












              0








              0







              awk '$3 ~ /^S/||/^M/||/^D/print $0' filename





              share|improve this answer













              awk '$3 ~ /^S/||/^M/||/^D/print $0' filename






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 9 at 17:56









              Praveen Kumar BSPraveen Kumar BS

              1,346138




              1,346138












              • The way this parses out will match column 3 starting with an S, or column 1 starting with an M or D -- it misses column 3 M/D and it incorrectly includes the column 1 matches.

                – Jeff Schaller
                Jan 9 at 19:52

















              • The way this parses out will match column 3 starting with an S, or column 1 starting with an M or D -- it misses column 3 M/D and it incorrectly includes the column 1 matches.

                – Jeff Schaller
                Jan 9 at 19:52
















              The way this parses out will match column 3 starting with an S, or column 1 starting with an M or D -- it misses column 3 M/D and it incorrectly includes the column 1 matches.

              – Jeff Schaller
              Jan 9 at 19:52





              The way this parses out will match column 3 starting with an S, or column 1 starting with an M or D -- it misses column 3 M/D and it incorrectly includes the column 1 matches.

              – Jeff Schaller
              Jan 9 at 19:52











              0














              Same has been achieved in python and it worked fine



              Code



              #!/usr/bin/python
              import subprocess
              import re
              h=re.compile(r'^[SMD]')
              o=open('l.txt','r')
              for i in o:
              j=i.split(':')
              if re.search(h,j[2]):
              print i.strip()





              share|improve this answer



























                0














                Same has been achieved in python and it worked fine



                Code



                #!/usr/bin/python
                import subprocess
                import re
                h=re.compile(r'^[SMD]')
                o=open('l.txt','r')
                for i in o:
                j=i.split(':')
                if re.search(h,j[2]):
                print i.strip()





                share|improve this answer

























                  0












                  0








                  0







                  Same has been achieved in python and it worked fine



                  Code



                  #!/usr/bin/python
                  import subprocess
                  import re
                  h=re.compile(r'^[SMD]')
                  o=open('l.txt','r')
                  for i in o:
                  j=i.split(':')
                  if re.search(h,j[2]):
                  print i.strip()





                  share|improve this answer













                  Same has been achieved in python and it worked fine



                  Code



                  #!/usr/bin/python
                  import subprocess
                  import re
                  h=re.compile(r'^[SMD]')
                  o=open('l.txt','r')
                  for i in o:
                  j=i.split(':')
                  if re.search(h,j[2]):
                  print i.strip()






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 13 at 6:23









                  Praveen Kumar BSPraveen Kumar BS

                  1,346138




                  1,346138



























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f492952%2fprint-entire-line-after-an-awk-and-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 check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay