find the Integer value after a string from a file





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{
margin-bottom:0;
}








2

















I want to get the integer value after a specific string from a file and sort all the integers.
Ex -
I have a file with thousands of lines with string weight,



-weight 100
-weight 200
-weight 20


I want to get all the integer values in sorted order.










share|improve this question



































    2

















    I want to get the integer value after a specific string from a file and sort all the integers.
    Ex -
    I have a file with thousands of lines with string weight,



    -weight 100
    -weight 200
    -weight 20


    I want to get all the integer values in sorted order.










    share|improve this question































      2












      2








      2








      I want to get the integer value after a specific string from a file and sort all the integers.
      Ex -
      I have a file with thousands of lines with string weight,



      -weight 100
      -weight 200
      -weight 20


      I want to get all the integer values in sorted order.










      share|improve this question

















      I want to get the integer value after a specific string from a file and sort all the integers.
      Ex -
      I have a file with thousands of lines with string weight,



      -weight 100
      -weight 200
      -weight 20


      I want to get all the integer values in sorted order.







      command-line text-processing






      share|improve this question
















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 29 at 8:00









      terdon

      74.7k14 gold badges151 silver badges235 bronze badges




      74.7k14 gold badges151 silver badges235 bronze badges










      asked May 28 at 13:11









      asad_nitpasad_nitp

      132 bronze badges




      132 bronze badges

























          3 Answers
          3






          active

          oldest

          votes


















          3


















          If you are searching for the longest string of numbers that is the last thing on the line, you can just use grep:



          $ grep -oP 'd+s*$' file 
          100
          200
          20


          The -o tells grep to only print the matching portion of the line and the -P enables Perl Compatible Regular Expressions. PCREs let us use d+ for "one or more digits" and s* for "0 or more whitespace characters". So, all together, that command will print the longest stretch of numbers found at the end of the line.



          If you need them sorted, just pass through sort:



          $ grep -oP 'd+s*$' file | sort -n
          20
          100
          200


          If, instead, you need to anchor your pattern using a specific string, use:



          $ grep -oP -- '-weights+Kd+' file | sort -n
          20
          100
          200


          The K tells grep not to include anything matched up to this point, so the command above will only print the longest stretch of numbers after the -weight and 0 or more spaces.



          Note that if you want to also include negative numbers or decimals, you will need:



          grep -oP -- '-weights+K[0-9,-]+' file | sort -n


          For example:



          $ cat file 
          -weight 100
          -weight 200
          -weight 20
          -weight -29
          -weight -32.4

          $ grep -oP -- '-weights+K[0-9,-]+' file | sort -n
          -32
          -29
          20
          100
          200





          share|improve this answer



























          • i had to remove K to print the output, otherwise there was'nt any output on the screen.

            – asad_nitp
            May 29 at 6:44











          • @asad_nitp that doesn't make sense, without the K you would be getting the whole line.

            – terdon
            May 29 at 8:00



















          3


















          Try this:



          cut -d ' ' -f2 inputfile|sort -n


          -d ' ' - set delimiter to space
          -f2 - get second column
          sort -n - sort result as numbers

          Test result:



          20
          100
          200





          share|improve this answer





























          • this works when there are only two columns, so i grep'd the -weight and value and then sorted using this method.

            – asad_nitp
            May 29 at 6:47











          • With a correct example answer will be different, so you will not need to grep. Provide an example then ping me, and I will change the answer.

            – LeonidMew
            May 29 at 8:28



















          3


















          Try this sed command:



          sed -E 's/^.*weights([[:digit:]]*).*/1/' sort.txt  | sort -n


          Or



          sed -E 's/.*weights([0-9]*).*/1/' sort.txt | sort -n


          Test case:



          -weight 100 
          -weight 200
          -weight 20


          Results:



          20
          100
          200


          Note:- You didn't give a good test case so I assumed mine.



          Info:





          • ^.*weights([[:digit:]]): Read each line and capture the numbers after the word "weight" with the "space" following.


          • | sort -n: Pipe the output into the sort command and sort by numeric value.






          share|improve this answer





























          • For the input shown, something like sed 's/[^0-9]*//' would be sufficient I think

            – steeldriver
            May 28 at 14:00













          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/4.0/"u003ecc by-sa 4.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%2f1146841%2ffind-the-integer-value-after-a-string-from-a-file%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


















          If you are searching for the longest string of numbers that is the last thing on the line, you can just use grep:



          $ grep -oP 'd+s*$' file 
          100
          200
          20


          The -o tells grep to only print the matching portion of the line and the -P enables Perl Compatible Regular Expressions. PCREs let us use d+ for "one or more digits" and s* for "0 or more whitespace characters". So, all together, that command will print the longest stretch of numbers found at the end of the line.



          If you need them sorted, just pass through sort:



          $ grep -oP 'd+s*$' file | sort -n
          20
          100
          200


          If, instead, you need to anchor your pattern using a specific string, use:



          $ grep -oP -- '-weights+Kd+' file | sort -n
          20
          100
          200


          The K tells grep not to include anything matched up to this point, so the command above will only print the longest stretch of numbers after the -weight and 0 or more spaces.



          Note that if you want to also include negative numbers or decimals, you will need:



          grep -oP -- '-weights+K[0-9,-]+' file | sort -n


          For example:



          $ cat file 
          -weight 100
          -weight 200
          -weight 20
          -weight -29
          -weight -32.4

          $ grep -oP -- '-weights+K[0-9,-]+' file | sort -n
          -32
          -29
          20
          100
          200





          share|improve this answer



























          • i had to remove K to print the output, otherwise there was'nt any output on the screen.

            – asad_nitp
            May 29 at 6:44











          • @asad_nitp that doesn't make sense, without the K you would be getting the whole line.

            – terdon
            May 29 at 8:00
















          3


















          If you are searching for the longest string of numbers that is the last thing on the line, you can just use grep:



          $ grep -oP 'd+s*$' file 
          100
          200
          20


          The -o tells grep to only print the matching portion of the line and the -P enables Perl Compatible Regular Expressions. PCREs let us use d+ for "one or more digits" and s* for "0 or more whitespace characters". So, all together, that command will print the longest stretch of numbers found at the end of the line.



          If you need them sorted, just pass through sort:



          $ grep -oP 'd+s*$' file | sort -n
          20
          100
          200


          If, instead, you need to anchor your pattern using a specific string, use:



          $ grep -oP -- '-weights+Kd+' file | sort -n
          20
          100
          200


          The K tells grep not to include anything matched up to this point, so the command above will only print the longest stretch of numbers after the -weight and 0 or more spaces.



          Note that if you want to also include negative numbers or decimals, you will need:



          grep -oP -- '-weights+K[0-9,-]+' file | sort -n


          For example:



          $ cat file 
          -weight 100
          -weight 200
          -weight 20
          -weight -29
          -weight -32.4

          $ grep -oP -- '-weights+K[0-9,-]+' file | sort -n
          -32
          -29
          20
          100
          200





          share|improve this answer



























          • i had to remove K to print the output, otherwise there was'nt any output on the screen.

            – asad_nitp
            May 29 at 6:44











          • @asad_nitp that doesn't make sense, without the K you would be getting the whole line.

            – terdon
            May 29 at 8:00














          3














          3










          3









          If you are searching for the longest string of numbers that is the last thing on the line, you can just use grep:



          $ grep -oP 'd+s*$' file 
          100
          200
          20


          The -o tells grep to only print the matching portion of the line and the -P enables Perl Compatible Regular Expressions. PCREs let us use d+ for "one or more digits" and s* for "0 or more whitespace characters". So, all together, that command will print the longest stretch of numbers found at the end of the line.



          If you need them sorted, just pass through sort:



          $ grep -oP 'd+s*$' file | sort -n
          20
          100
          200


          If, instead, you need to anchor your pattern using a specific string, use:



          $ grep -oP -- '-weights+Kd+' file | sort -n
          20
          100
          200


          The K tells grep not to include anything matched up to this point, so the command above will only print the longest stretch of numbers after the -weight and 0 or more spaces.



          Note that if you want to also include negative numbers or decimals, you will need:



          grep -oP -- '-weights+K[0-9,-]+' file | sort -n


          For example:



          $ cat file 
          -weight 100
          -weight 200
          -weight 20
          -weight -29
          -weight -32.4

          $ grep -oP -- '-weights+K[0-9,-]+' file | sort -n
          -32
          -29
          20
          100
          200





          share|improve this answer














          If you are searching for the longest string of numbers that is the last thing on the line, you can just use grep:



          $ grep -oP 'd+s*$' file 
          100
          200
          20


          The -o tells grep to only print the matching portion of the line and the -P enables Perl Compatible Regular Expressions. PCREs let us use d+ for "one or more digits" and s* for "0 or more whitespace characters". So, all together, that command will print the longest stretch of numbers found at the end of the line.



          If you need them sorted, just pass through sort:



          $ grep -oP 'd+s*$' file | sort -n
          20
          100
          200


          If, instead, you need to anchor your pattern using a specific string, use:



          $ grep -oP -- '-weights+Kd+' file | sort -n
          20
          100
          200


          The K tells grep not to include anything matched up to this point, so the command above will only print the longest stretch of numbers after the -weight and 0 or more spaces.



          Note that if you want to also include negative numbers or decimals, you will need:



          grep -oP -- '-weights+K[0-9,-]+' file | sort -n


          For example:



          $ cat file 
          -weight 100
          -weight 200
          -weight 20
          -weight -29
          -weight -32.4

          $ grep -oP -- '-weights+K[0-9,-]+' file | sort -n
          -32
          -29
          20
          100
          200






          share|improve this answer













          share|improve this answer




          share|improve this answer










          answered May 28 at 14:34









          terdonterdon

          74.7k14 gold badges151 silver badges235 bronze badges




          74.7k14 gold badges151 silver badges235 bronze badges
















          • i had to remove K to print the output, otherwise there was'nt any output on the screen.

            – asad_nitp
            May 29 at 6:44











          • @asad_nitp that doesn't make sense, without the K you would be getting the whole line.

            – terdon
            May 29 at 8:00



















          • i had to remove K to print the output, otherwise there was'nt any output on the screen.

            – asad_nitp
            May 29 at 6:44











          • @asad_nitp that doesn't make sense, without the K you would be getting the whole line.

            – terdon
            May 29 at 8:00

















          i had to remove K to print the output, otherwise there was'nt any output on the screen.

          – asad_nitp
          May 29 at 6:44





          i had to remove K to print the output, otherwise there was'nt any output on the screen.

          – asad_nitp
          May 29 at 6:44













          @asad_nitp that doesn't make sense, without the K you would be getting the whole line.

          – terdon
          May 29 at 8:00





          @asad_nitp that doesn't make sense, without the K you would be getting the whole line.

          – terdon
          May 29 at 8:00













          3


















          Try this:



          cut -d ' ' -f2 inputfile|sort -n


          -d ' ' - set delimiter to space
          -f2 - get second column
          sort -n - sort result as numbers

          Test result:



          20
          100
          200





          share|improve this answer





























          • this works when there are only two columns, so i grep'd the -weight and value and then sorted using this method.

            – asad_nitp
            May 29 at 6:47











          • With a correct example answer will be different, so you will not need to grep. Provide an example then ping me, and I will change the answer.

            – LeonidMew
            May 29 at 8:28
















          3


















          Try this:



          cut -d ' ' -f2 inputfile|sort -n


          -d ' ' - set delimiter to space
          -f2 - get second column
          sort -n - sort result as numbers

          Test result:



          20
          100
          200





          share|improve this answer





























          • this works when there are only two columns, so i grep'd the -weight and value and then sorted using this method.

            – asad_nitp
            May 29 at 6:47











          • With a correct example answer will be different, so you will not need to grep. Provide an example then ping me, and I will change the answer.

            – LeonidMew
            May 29 at 8:28














          3














          3










          3









          Try this:



          cut -d ' ' -f2 inputfile|sort -n


          -d ' ' - set delimiter to space
          -f2 - get second column
          sort -n - sort result as numbers

          Test result:



          20
          100
          200





          share|improve this answer
















          Try this:



          cut -d ' ' -f2 inputfile|sort -n


          -d ' ' - set delimiter to space
          -f2 - get second column
          sort -n - sort result as numbers

          Test result:



          20
          100
          200






          share|improve this answer















          share|improve this answer




          share|improve this answer








          edited May 28 at 13:59

























          answered May 28 at 13:51









          LeonidMewLeonidMew

          1,99012 silver badges28 bronze badges




          1,99012 silver badges28 bronze badges
















          • this works when there are only two columns, so i grep'd the -weight and value and then sorted using this method.

            – asad_nitp
            May 29 at 6:47











          • With a correct example answer will be different, so you will not need to grep. Provide an example then ping me, and I will change the answer.

            – LeonidMew
            May 29 at 8:28



















          • this works when there are only two columns, so i grep'd the -weight and value and then sorted using this method.

            – asad_nitp
            May 29 at 6:47











          • With a correct example answer will be different, so you will not need to grep. Provide an example then ping me, and I will change the answer.

            – LeonidMew
            May 29 at 8:28

















          this works when there are only two columns, so i grep'd the -weight and value and then sorted using this method.

          – asad_nitp
          May 29 at 6:47





          this works when there are only two columns, so i grep'd the -weight and value and then sorted using this method.

          – asad_nitp
          May 29 at 6:47













          With a correct example answer will be different, so you will not need to grep. Provide an example then ping me, and I will change the answer.

          – LeonidMew
          May 29 at 8:28





          With a correct example answer will be different, so you will not need to grep. Provide an example then ping me, and I will change the answer.

          – LeonidMew
          May 29 at 8:28











          3


















          Try this sed command:



          sed -E 's/^.*weights([[:digit:]]*).*/1/' sort.txt  | sort -n


          Or



          sed -E 's/.*weights([0-9]*).*/1/' sort.txt | sort -n


          Test case:



          -weight 100 
          -weight 200
          -weight 20


          Results:



          20
          100
          200


          Note:- You didn't give a good test case so I assumed mine.



          Info:





          • ^.*weights([[:digit:]]): Read each line and capture the numbers after the word "weight" with the "space" following.


          • | sort -n: Pipe the output into the sort command and sort by numeric value.






          share|improve this answer





























          • For the input shown, something like sed 's/[^0-9]*//' would be sufficient I think

            – steeldriver
            May 28 at 14:00
















          3


















          Try this sed command:



          sed -E 's/^.*weights([[:digit:]]*).*/1/' sort.txt  | sort -n


          Or



          sed -E 's/.*weights([0-9]*).*/1/' sort.txt | sort -n


          Test case:



          -weight 100 
          -weight 200
          -weight 20


          Results:



          20
          100
          200


          Note:- You didn't give a good test case so I assumed mine.



          Info:





          • ^.*weights([[:digit:]]): Read each line and capture the numbers after the word "weight" with the "space" following.


          • | sort -n: Pipe the output into the sort command and sort by numeric value.






          share|improve this answer





























          • For the input shown, something like sed 's/[^0-9]*//' would be sufficient I think

            – steeldriver
            May 28 at 14:00














          3














          3










          3









          Try this sed command:



          sed -E 's/^.*weights([[:digit:]]*).*/1/' sort.txt  | sort -n


          Or



          sed -E 's/.*weights([0-9]*).*/1/' sort.txt | sort -n


          Test case:



          -weight 100 
          -weight 200
          -weight 20


          Results:



          20
          100
          200


          Note:- You didn't give a good test case so I assumed mine.



          Info:





          • ^.*weights([[:digit:]]): Read each line and capture the numbers after the word "weight" with the "space" following.


          • | sort -n: Pipe the output into the sort command and sort by numeric value.






          share|improve this answer
















          Try this sed command:



          sed -E 's/^.*weights([[:digit:]]*).*/1/' sort.txt  | sort -n


          Or



          sed -E 's/.*weights([0-9]*).*/1/' sort.txt | sort -n


          Test case:



          -weight 100 
          -weight 200
          -weight 20


          Results:



          20
          100
          200


          Note:- You didn't give a good test case so I assumed mine.



          Info:





          • ^.*weights([[:digit:]]): Read each line and capture the numbers after the word "weight" with the "space" following.


          • | sort -n: Pipe the output into the sort command and sort by numeric value.







          share|improve this answer















          share|improve this answer




          share|improve this answer








          edited May 28 at 14:07

























          answered May 28 at 13:31









          George UdosenGeorge Udosen

          24.3k11 gold badges60 silver badges82 bronze badges




          24.3k11 gold badges60 silver badges82 bronze badges
















          • For the input shown, something like sed 's/[^0-9]*//' would be sufficient I think

            – steeldriver
            May 28 at 14:00



















          • For the input shown, something like sed 's/[^0-9]*//' would be sufficient I think

            – steeldriver
            May 28 at 14:00

















          For the input shown, something like sed 's/[^0-9]*//' would be sufficient I think

          – steeldriver
          May 28 at 14:00





          For the input shown, something like sed 's/[^0-9]*//' would be sufficient I think

          – steeldriver
          May 28 at 14:00



















          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%2f1146841%2ffind-the-integer-value-after-a-string-from-a-file%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

          He _____ here since 1970 . Answer needed [closed]What does “since he was so high” mean?Meaning of “catch birds for”?How do I ensure “since” takes the meaning I want?“Who cares here” meaningWhat does “right round toward” mean?the time tense (had now been detected)What does the phrase “ring around the roses” mean here?Correct usage of “visited upon”Meaning of “foiled rail sabotage bid”It was the third time I had gone to Rome or It is the third time I had been to Rome

          Bunad

          Færeyskur hestur Heimild | Tengill | Tilvísanir | LeiðsagnarvalRossið - síða um færeyska hrossið á færeyskuGott ár hjá færeyska hestinum