grep specific number from lines 5 but not 25 or 52 and so on












3















I need to find a user for a userid. The return from the authentication system is as follows. Now with a bash script I need to extract the line with the exact number. Not 5 and 25 ..



------------
ID LOGIN
------------
28 user1
25 user2
5 user3


If I use grep 5 I do get 2 lines but I need the line with "5".



Any ideas ?










share|improve this question





























    3















    I need to find a user for a userid. The return from the authentication system is as follows. Now with a bash script I need to extract the line with the exact number. Not 5 and 25 ..



    ------------
    ID LOGIN
    ------------
    28 user1
    25 user2
    5 user3


    If I use grep 5 I do get 2 lines but I need the line with "5".



    Any ideas ?










    share|improve this question



























      3












      3








      3








      I need to find a user for a userid. The return from the authentication system is as follows. Now with a bash script I need to extract the line with the exact number. Not 5 and 25 ..



      ------------
      ID LOGIN
      ------------
      28 user1
      25 user2
      5 user3


      If I use grep 5 I do get 2 lines but I need the line with "5".



      Any ideas ?










      share|improve this question
















      I need to find a user for a userid. The return from the authentication system is as follows. Now with a bash script I need to extract the line with the exact number. Not 5 and 25 ..



      ------------
      ID LOGIN
      ------------
      28 user1
      25 user2
      5 user3


      If I use grep 5 I do get 2 lines but I need the line with "5".



      Any ideas ?







      text-processing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 9 at 12:15









      don_crissti

      50.3k15134162




      50.3k15134162










      asked Jan 9 at 12:13









      Dave.Dave.

      241




      241






















          3 Answers
          3






          active

          oldest

          votes


















          8














          There are several ways of doing it.



          IMHO the best way is to use awk, which is useful when dealing with fields.



          If you want a grep based solution, I would do:



          grep -w '^5'


          The -w tells grep to match the exact word, so this will not match "52". The "^" tells grep to search the 5 at the beginning of the line, which will fail if there are e.g. leading spaces.



          The awk solution would look like:



          awk '$1 == 5'


          If you want only the username, which is the second column:



          awk '$1 == 5 {print $2}'


          If you're searching for a string and not a numeric value, enclose the string in double quotes:



          awk '$1 == "abc" {print $2}'





          share|improve this answer





















          • 1





            thanks I have choosen the awk way. awk '$1 == 5 {print $2}' that worked.

            – Dave.
            Jan 9 at 14:05








          • 2





            @Dave. Since this answer worked for you, please consider clicking the checkbox beside it to signal to future readers it answered your question.

            – bishop
            Jan 9 at 20:59



















          2














          You could try with a regex (first char in line) and including the space:



          grep -E "^5 "





          share|improve this answer



















          • 4





            This would be better as grep -E "^5s" in case there's a tab and not a space.

            – terdon
            Jan 9 at 13:50



















          0














          Try the following:



          query-auth-system | grep "^5\>"




          • ^: means "match at start of line


          • \>: matches a word-boundary. So it will match 5, but not 50.






          share|improve this answer



















          • 2





            That will also match lines starting with 5:, 5%, 5| etc.

            – terdon
            Jan 9 at 13:49











          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%2f493445%2fgrep-specific-number-from-lines-5-but-not-25-or-52-and-so-on%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









          8














          There are several ways of doing it.



          IMHO the best way is to use awk, which is useful when dealing with fields.



          If you want a grep based solution, I would do:



          grep -w '^5'


          The -w tells grep to match the exact word, so this will not match "52". The "^" tells grep to search the 5 at the beginning of the line, which will fail if there are e.g. leading spaces.



          The awk solution would look like:



          awk '$1 == 5'


          If you want only the username, which is the second column:



          awk '$1 == 5 {print $2}'


          If you're searching for a string and not a numeric value, enclose the string in double quotes:



          awk '$1 == "abc" {print $2}'





          share|improve this answer





















          • 1





            thanks I have choosen the awk way. awk '$1 == 5 {print $2}' that worked.

            – Dave.
            Jan 9 at 14:05








          • 2





            @Dave. Since this answer worked for you, please consider clicking the checkbox beside it to signal to future readers it answered your question.

            – bishop
            Jan 9 at 20:59
















          8














          There are several ways of doing it.



          IMHO the best way is to use awk, which is useful when dealing with fields.



          If you want a grep based solution, I would do:



          grep -w '^5'


          The -w tells grep to match the exact word, so this will not match "52". The "^" tells grep to search the 5 at the beginning of the line, which will fail if there are e.g. leading spaces.



          The awk solution would look like:



          awk '$1 == 5'


          If you want only the username, which is the second column:



          awk '$1 == 5 {print $2}'


          If you're searching for a string and not a numeric value, enclose the string in double quotes:



          awk '$1 == "abc" {print $2}'





          share|improve this answer





















          • 1





            thanks I have choosen the awk way. awk '$1 == 5 {print $2}' that worked.

            – Dave.
            Jan 9 at 14:05








          • 2





            @Dave. Since this answer worked for you, please consider clicking the checkbox beside it to signal to future readers it answered your question.

            – bishop
            Jan 9 at 20:59














          8












          8








          8







          There are several ways of doing it.



          IMHO the best way is to use awk, which is useful when dealing with fields.



          If you want a grep based solution, I would do:



          grep -w '^5'


          The -w tells grep to match the exact word, so this will not match "52". The "^" tells grep to search the 5 at the beginning of the line, which will fail if there are e.g. leading spaces.



          The awk solution would look like:



          awk '$1 == 5'


          If you want only the username, which is the second column:



          awk '$1 == 5 {print $2}'


          If you're searching for a string and not a numeric value, enclose the string in double quotes:



          awk '$1 == "abc" {print $2}'





          share|improve this answer















          There are several ways of doing it.



          IMHO the best way is to use awk, which is useful when dealing with fields.



          If you want a grep based solution, I would do:



          grep -w '^5'


          The -w tells grep to match the exact word, so this will not match "52". The "^" tells grep to search the 5 at the beginning of the line, which will fail if there are e.g. leading spaces.



          The awk solution would look like:



          awk '$1 == 5'


          If you want only the username, which is the second column:



          awk '$1 == 5 {print $2}'


          If you're searching for a string and not a numeric value, enclose the string in double quotes:



          awk '$1 == "abc" {print $2}'






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 12 at 19:22









          Jeff Schaller

          39.5k1054126




          39.5k1054126










          answered Jan 9 at 12:24









          wurtelwurtel

          10k11425




          10k11425








          • 1





            thanks I have choosen the awk way. awk '$1 == 5 {print $2}' that worked.

            – Dave.
            Jan 9 at 14:05








          • 2





            @Dave. Since this answer worked for you, please consider clicking the checkbox beside it to signal to future readers it answered your question.

            – bishop
            Jan 9 at 20:59














          • 1





            thanks I have choosen the awk way. awk '$1 == 5 {print $2}' that worked.

            – Dave.
            Jan 9 at 14:05








          • 2





            @Dave. Since this answer worked for you, please consider clicking the checkbox beside it to signal to future readers it answered your question.

            – bishop
            Jan 9 at 20:59








          1




          1





          thanks I have choosen the awk way. awk '$1 == 5 {print $2}' that worked.

          – Dave.
          Jan 9 at 14:05







          thanks I have choosen the awk way. awk '$1 == 5 {print $2}' that worked.

          – Dave.
          Jan 9 at 14:05






          2




          2





          @Dave. Since this answer worked for you, please consider clicking the checkbox beside it to signal to future readers it answered your question.

          – bishop
          Jan 9 at 20:59





          @Dave. Since this answer worked for you, please consider clicking the checkbox beside it to signal to future readers it answered your question.

          – bishop
          Jan 9 at 20:59













          2














          You could try with a regex (first char in line) and including the space:



          grep -E "^5 "





          share|improve this answer



















          • 4





            This would be better as grep -E "^5s" in case there's a tab and not a space.

            – terdon
            Jan 9 at 13:50
















          2














          You could try with a regex (first char in line) and including the space:



          grep -E "^5 "





          share|improve this answer



















          • 4





            This would be better as grep -E "^5s" in case there's a tab and not a space.

            – terdon
            Jan 9 at 13:50














          2












          2








          2







          You could try with a regex (first char in line) and including the space:



          grep -E "^5 "





          share|improve this answer













          You could try with a regex (first char in line) and including the space:



          grep -E "^5 "






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 9 at 12:21









          rbrtflrrbrtflr

          613




          613








          • 4





            This would be better as grep -E "^5s" in case there's a tab and not a space.

            – terdon
            Jan 9 at 13:50














          • 4





            This would be better as grep -E "^5s" in case there's a tab and not a space.

            – terdon
            Jan 9 at 13:50








          4




          4





          This would be better as grep -E "^5s" in case there's a tab and not a space.

          – terdon
          Jan 9 at 13:50





          This would be better as grep -E "^5s" in case there's a tab and not a space.

          – terdon
          Jan 9 at 13:50











          0














          Try the following:



          query-auth-system | grep "^5\>"




          • ^: means "match at start of line


          • \>: matches a word-boundary. So it will match 5, but not 50.






          share|improve this answer



















          • 2





            That will also match lines starting with 5:, 5%, 5| etc.

            – terdon
            Jan 9 at 13:49
















          0














          Try the following:



          query-auth-system | grep "^5\>"




          • ^: means "match at start of line


          • \>: matches a word-boundary. So it will match 5, but not 50.






          share|improve this answer



















          • 2





            That will also match lines starting with 5:, 5%, 5| etc.

            – terdon
            Jan 9 at 13:49














          0












          0








          0







          Try the following:



          query-auth-system | grep "^5\>"




          • ^: means "match at start of line


          • \>: matches a word-boundary. So it will match 5, but not 50.






          share|improve this answer













          Try the following:



          query-auth-system | grep "^5\>"




          • ^: means "match at start of line


          • \>: matches a word-boundary. So it will match 5, but not 50.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 9 at 12:19









          RalfRalf

          3277




          3277








          • 2





            That will also match lines starting with 5:, 5%, 5| etc.

            – terdon
            Jan 9 at 13:49














          • 2





            That will also match lines starting with 5:, 5%, 5| etc.

            – terdon
            Jan 9 at 13:49








          2




          2





          That will also match lines starting with 5:, 5%, 5| etc.

          – terdon
          Jan 9 at 13:49





          That will also match lines starting with 5:, 5%, 5| etc.

          – terdon
          Jan 9 at 13:49


















          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%2f493445%2fgrep-specific-number-from-lines-5-but-not-25-or-52-and-so-on%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

          Mario Kart Wii

          What does “Dominus providebit” mean?

          Antonio Litta Visconti Arese