Conditional grepping

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











up vote
0
down vote

favorite












I have a config file and its content looks something like below:



 Jobname|Type|Silo|Description
#comment1
#comment2
job1|me|silo1|test_job1
job1|me|silo1|test_job2
job1|prod|silo1|test_job3


Now I need the conditional content of the file, say the content with TYPE =me.
For this I am using a grep with me:



 job_detail=$((cat config_file | grep me | awk 'print $4'))


In this case, I am getting the first line also , as JOBNAME is getting matching character me.
I escaped comments with -v options. I can't comment the first line of the config file, as it is used by other unknown processes.



Is there a way i can grep the whole word match? would be better if there is a way to grep whole word with a particular character as condition.



A way to divide the line with '|' and then grep?










share|improve this question



















  • 5




    Why not just awk -F| '$2=="me" print $4' config_file?
    – steeldriver
    Dec 4 '15 at 4:01










  • @steeldriver... thanx... this works, but suppose i need "me" to be resolved from a variable,say $region, how this should be applied with above?
    – NishantM
    Dec 7 '15 at 8:32










  • awk -F| -v region="$region" '$2==region print $4' config_file
    – steeldriver
    Dec 7 '15 at 12:49















up vote
0
down vote

favorite












I have a config file and its content looks something like below:



 Jobname|Type|Silo|Description
#comment1
#comment2
job1|me|silo1|test_job1
job1|me|silo1|test_job2
job1|prod|silo1|test_job3


Now I need the conditional content of the file, say the content with TYPE =me.
For this I am using a grep with me:



 job_detail=$((cat config_file | grep me | awk 'print $4'))


In this case, I am getting the first line also , as JOBNAME is getting matching character me.
I escaped comments with -v options. I can't comment the first line of the config file, as it is used by other unknown processes.



Is there a way i can grep the whole word match? would be better if there is a way to grep whole word with a particular character as condition.



A way to divide the line with '|' and then grep?










share|improve this question



















  • 5




    Why not just awk -F| '$2=="me" print $4' config_file?
    – steeldriver
    Dec 4 '15 at 4:01










  • @steeldriver... thanx... this works, but suppose i need "me" to be resolved from a variable,say $region, how this should be applied with above?
    – NishantM
    Dec 7 '15 at 8:32










  • awk -F| -v region="$region" '$2==region print $4' config_file
    – steeldriver
    Dec 7 '15 at 12:49













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a config file and its content looks something like below:



 Jobname|Type|Silo|Description
#comment1
#comment2
job1|me|silo1|test_job1
job1|me|silo1|test_job2
job1|prod|silo1|test_job3


Now I need the conditional content of the file, say the content with TYPE =me.
For this I am using a grep with me:



 job_detail=$((cat config_file | grep me | awk 'print $4'))


In this case, I am getting the first line also , as JOBNAME is getting matching character me.
I escaped comments with -v options. I can't comment the first line of the config file, as it is used by other unknown processes.



Is there a way i can grep the whole word match? would be better if there is a way to grep whole word with a particular character as condition.



A way to divide the line with '|' and then grep?










share|improve this question















I have a config file and its content looks something like below:



 Jobname|Type|Silo|Description
#comment1
#comment2
job1|me|silo1|test_job1
job1|me|silo1|test_job2
job1|prod|silo1|test_job3


Now I need the conditional content of the file, say the content with TYPE =me.
For this I am using a grep with me:



 job_detail=$((cat config_file | grep me | awk 'print $4'))


In this case, I am getting the first line also , as JOBNAME is getting matching character me.
I escaped comments with -v options. I can't comment the first line of the config file, as it is used by other unknown processes.



Is there a way i can grep the whole word match? would be better if there is a way to grep whole word with a particular character as condition.



A way to divide the line with '|' and then grep?







shell-script awk scripting grep pipe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 at 20:54









Rui F Ribeiro

38.2k1475123




38.2k1475123










asked Dec 4 '15 at 3:54









NishantM

37




37







  • 5




    Why not just awk -F| '$2=="me" print $4' config_file?
    – steeldriver
    Dec 4 '15 at 4:01










  • @steeldriver... thanx... this works, but suppose i need "me" to be resolved from a variable,say $region, how this should be applied with above?
    – NishantM
    Dec 7 '15 at 8:32










  • awk -F| -v region="$region" '$2==region print $4' config_file
    – steeldriver
    Dec 7 '15 at 12:49













  • 5




    Why not just awk -F| '$2=="me" print $4' config_file?
    – steeldriver
    Dec 4 '15 at 4:01










  • @steeldriver... thanx... this works, but suppose i need "me" to be resolved from a variable,say $region, how this should be applied with above?
    – NishantM
    Dec 7 '15 at 8:32










  • awk -F| -v region="$region" '$2==region print $4' config_file
    – steeldriver
    Dec 7 '15 at 12:49








5




5




Why not just awk -F| '$2=="me" print $4' config_file?
– steeldriver
Dec 4 '15 at 4:01




Why not just awk -F| '$2=="me" print $4' config_file?
– steeldriver
Dec 4 '15 at 4:01












@steeldriver... thanx... this works, but suppose i need "me" to be resolved from a variable,say $region, how this should be applied with above?
– NishantM
Dec 7 '15 at 8:32




@steeldriver... thanx... this works, but suppose i need "me" to be resolved from a variable,say $region, how this should be applied with above?
– NishantM
Dec 7 '15 at 8:32












awk -F| -v region="$region" '$2==region print $4' config_file
– steeldriver
Dec 7 '15 at 12:49





awk -F| -v region="$region" '$2==region print $4' config_file
– steeldriver
Dec 7 '15 at 12:49











3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










try



awk -F| -v select="$var" '$2 == select print $4;' config_file


where




  • $var contains the field you want to select

  • -F| tell awk to use | as separator, | (pipr) must be escaped.

  • -v select="$var" transfer $var to awk variable (select)


  • $2 == select select line whose second arg is "$var" or select.


  • print $4; print fourth field.





share|improve this answer




















  • wow.... thanxs @archemar.... - v select worked like a charm for me... thanxs again
    – NishantM
    Dec 7 '15 at 8:35

















up vote
0
down vote













man grep will show you the -w flag:



-w, --word-regexp Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore. 


Or, stick | egrep -v Jobname early in your pipline.






share|improve this answer



























    up vote
    0
    down vote













    A variation of Archemar's solution which assumes that the me that you want to search for is $LOGNAME, i.e. the username of the current user:



    awk -F '|' '$2 == ENVIRON["LOGNAME"] print $4 ' <config_file





    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: 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%2f247279%2fconditional-grepping%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








      up vote
      2
      down vote



      accepted










      try



      awk -F| -v select="$var" '$2 == select print $4;' config_file


      where




      • $var contains the field you want to select

      • -F| tell awk to use | as separator, | (pipr) must be escaped.

      • -v select="$var" transfer $var to awk variable (select)


      • $2 == select select line whose second arg is "$var" or select.


      • print $4; print fourth field.





      share|improve this answer




















      • wow.... thanxs @archemar.... - v select worked like a charm for me... thanxs again
        – NishantM
        Dec 7 '15 at 8:35














      up vote
      2
      down vote



      accepted










      try



      awk -F| -v select="$var" '$2 == select print $4;' config_file


      where




      • $var contains the field you want to select

      • -F| tell awk to use | as separator, | (pipr) must be escaped.

      • -v select="$var" transfer $var to awk variable (select)


      • $2 == select select line whose second arg is "$var" or select.


      • print $4; print fourth field.





      share|improve this answer




















      • wow.... thanxs @archemar.... - v select worked like a charm for me... thanxs again
        – NishantM
        Dec 7 '15 at 8:35












      up vote
      2
      down vote



      accepted







      up vote
      2
      down vote



      accepted






      try



      awk -F| -v select="$var" '$2 == select print $4;' config_file


      where




      • $var contains the field you want to select

      • -F| tell awk to use | as separator, | (pipr) must be escaped.

      • -v select="$var" transfer $var to awk variable (select)


      • $2 == select select line whose second arg is "$var" or select.


      • print $4; print fourth field.





      share|improve this answer












      try



      awk -F| -v select="$var" '$2 == select print $4;' config_file


      where




      • $var contains the field you want to select

      • -F| tell awk to use | as separator, | (pipr) must be escaped.

      • -v select="$var" transfer $var to awk variable (select)


      • $2 == select select line whose second arg is "$var" or select.


      • print $4; print fourth field.






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Dec 4 '15 at 7:19









      Archemar

      19.4k93468




      19.4k93468











      • wow.... thanxs @archemar.... - v select worked like a charm for me... thanxs again
        – NishantM
        Dec 7 '15 at 8:35
















      • wow.... thanxs @archemar.... - v select worked like a charm for me... thanxs again
        – NishantM
        Dec 7 '15 at 8:35















      wow.... thanxs @archemar.... - v select worked like a charm for me... thanxs again
      – NishantM
      Dec 7 '15 at 8:35




      wow.... thanxs @archemar.... - v select worked like a charm for me... thanxs again
      – NishantM
      Dec 7 '15 at 8:35












      up vote
      0
      down vote













      man grep will show you the -w flag:



      -w, --word-regexp Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore. 


      Or, stick | egrep -v Jobname early in your pipline.






      share|improve this answer
























        up vote
        0
        down vote













        man grep will show you the -w flag:



        -w, --word-regexp Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore. 


        Or, stick | egrep -v Jobname early in your pipline.






        share|improve this answer






















          up vote
          0
          down vote










          up vote
          0
          down vote









          man grep will show you the -w flag:



          -w, --word-regexp Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore. 


          Or, stick | egrep -v Jobname early in your pipline.






          share|improve this answer












          man grep will show you the -w flag:



          -w, --word-regexp Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore. 


          Or, stick | egrep -v Jobname early in your pipline.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 4 '15 at 7:03









          waltinator

          71048




          71048




















              up vote
              0
              down vote













              A variation of Archemar's solution which assumes that the me that you want to search for is $LOGNAME, i.e. the username of the current user:



              awk -F '|' '$2 == ENVIRON["LOGNAME"] print $4 ' <config_file





              share|improve this answer
























                up vote
                0
                down vote













                A variation of Archemar's solution which assumes that the me that you want to search for is $LOGNAME, i.e. the username of the current user:



                awk -F '|' '$2 == ENVIRON["LOGNAME"] print $4 ' <config_file





                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  A variation of Archemar's solution which assumes that the me that you want to search for is $LOGNAME, i.e. the username of the current user:



                  awk -F '|' '$2 == ENVIRON["LOGNAME"] print $4 ' <config_file





                  share|improve this answer












                  A variation of Archemar's solution which assumes that the me that you want to search for is $LOGNAME, i.e. the username of the current user:



                  awk -F '|' '$2 == ENVIRON["LOGNAME"] print $4 ' <config_file






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 17 at 20:58









                  Kusalananda

                  116k15218352




                  116k15218352



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f247279%2fconditional-grepping%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?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?