find the Integer value after a string from a fileFind out if lines of a file is sortedextract text from a file using terminal?How to delete all lines before and after the specific strings in the file?How to remove all lines in a file containing a string from another fileHow can I only extract lines from a file if column 1 occurs at least n- times?Extracting a specific string after a given string from HTML file using a bash scriptapache2 : How to search a string from apache2 error logs in specific time range?For all files in folder with string in name, add value in cell (2,9) to the end of file name

What is the white stuff emerging from a hole on the surface of Mars?

What does "away to insignificance" mean?

Did Bobby Fischer actually write "Bobby Fischer Teaches Chess"

How to run fortran77 program with inputs from file?

Function of って in this sentence

Is Mercy Inverse Injustice or Inversed Injustice?

Why apt asking to uninstall GIMP when installing ardour?

Matrix class in C#

Modeling the Round (Nearest Integer) function

Is this really played by 2200+ players?

When and why did the House rules change to permit an inquiry without a vote?

What do you do when brand new pads are rubbing on new rotor? It smokes and smells so bad and it seems like it is going to catch on fire

How to write Gaussian reduction?

Would fantasy dwarves be able to invent and/or manufacture the radio?

Convexity of a QP

Was Hitler exclaiming "Heil Hitler!" himself when saluting?

PUTTY - Most insecure SSH client?

How do social media apps notify you when someone else takes a screenshot of your profile?

Make a list of Fibonacci numbers less than 1000, whose index is in the form 3m+1. Verify that all these numbers are odd

In this scene from the novel, 'The Martian', by Andy Weir, how does Mark Watney store hydrogen made from water in the tank?

What kind of mathematical disciplines would be most useful for physics?

What does "he was equally game to slip into bit parts" mean?

Why didn't Petunia know that Harry wasn't supposed to use magic out of school?

How would a race of humanoids with tails design [vehicle] seats?



find the Integer value after a string from a file


Find out if lines of a file is sortedextract text from a file using terminal?How to delete all lines before and after the specific strings in the file?How to remove all lines in a file containing a string from another fileHow can I only extract lines from a file if column 1 occurs at least n- times?Extracting a specific string after a given string from HTML file using a bash scriptapache2 : How to search a string from apache2 error logs in specific time range?For all files in folder with string in name, add value in cell (2,9) to the end of file name






.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.8k14 gold badges151 silver badges235 bronze badges




      74.8k14 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.8k14 gold badges151 silver badges235 bronze badges




          74.8k14 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

          Tamil (spriik) Luke uk diar | Nawigatjuun

          Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

          Training a classifier when some of the features are unknownWhy does Gradient Boosting regression predict negative values when there are no negative y-values in my training set?How to improve an existing (trained) classifier?What is effect when I set up some self defined predisctor variables?Why Matlab neural network classification returns decimal values on prediction dataset?Fitting and transforming text data in training, testing, and validation setsHow to quantify the performance of the classifier (multi-class SVM) using the test data?How do I control for some patients providing multiple samples in my training data?Training and Test setTraining a convolutional neural network for image denoising in MatlabShouldn't an autoencoder with #(neurons in hidden layer) = #(neurons in input layer) be “perfect”?