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?How to create a Bash script which can search for a string in a file and get the numeric value of the string and compare with another valueFor all files in folder with string in name, add value in cell (2,9) to the end of file nameBash convert string to integer

Why does there seem to be an extreme lack of public trashcans in Taiwan?

Nth term of Van Eck Sequence

When to use the uncountable form of a noun?

Can I attach a DC blower to intake manifold of my 150CC Yamaha FZS FI engine?

Convert GE Load Center to main breaker

David slept with Bathsheba because she was pure?? What does that mean?

Is Jesus the last Prophet?

Undocumented incompatibility between changes and siunitx?

What does BREAD stand for while drafting?

Which are the methodologies for interpreting Vedas?

As easy as Three, Two, One... How fast can you go from Five to Four?

Are skill challenges an official option or homebrewed?

Why didn't all the iron and heavier elements find their way to the center of the accretion disc in the early solar system?

Is all-caps blackletter no longer taboo?

Is it good practice to create tables dynamically?

About the paper by Buekenhout, Delandtsheer, Doyen, Kleidman, Liebeck and Saxl

What is the language spoken in Babylon?

Can I get a photo of an Ancient Arrow?

In The Incredibles 2, why does Screenslaver's name use a pun on something that doesn't exist in the 1950s pastiche?

How to deal with an excess of white-space in a CRM UI?

Why would a home insurer offer a discount based on credit score?

Dedicated bike GPS computer over smartphone

Part of my house is inexplicably gone

Is tuition reimbursement a good idea if you have to stay with the job



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?How to create a Bash script which can search for a string in a file and get the numeric value of the string and compare with another valueFor all files in folder with string in name, add value in cell (2,9) to the end of file nameBash convert string to integer






.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

      70.4k13147231




      70.4k13147231










      asked May 28 at 13:11









      asad_nitpasad_nitp

      132




      132




















          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/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%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

          70.4k13147231




          70.4k13147231












          • 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,8851127




          1,8851127












          • 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

          22.7k105177




          22.7k105177












          • 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

          How to write a 12-bar blues melodyI-IV-V blues progressionHow to play the bridges in a standard blues progressionHow does Gdim7 fit in C# minor?question on a certain chord progressionMusicology of Melody12 bar blues, spread rhythm: alternative to 6th chord to avoid finger stretchChord progressions/ Root key/ MelodiesHow to put chords (POP-EDM) under a given lead vocal melody (starting from a good knowledge in music theory)Are there “rules” for improvising with the minor pentatonic scale over 12-bar shuffle?Confusion about blues scale and chords

          What if the end-user didn't have the required library?What is setup.py?What is a clean, pythonic way to have multiple constructors in Python?What does Ruby have that Python doesn't, and vice versa?What is the reason for having '//' in Python?How do I create a namespace package in Python?How to package shared objects that python modules depend on?setuptools vs. distutils: why is distutils still a thing?Navigation in Windows 10 vs code not going to virtualenv library when the same library is installed at user levelPython create package for local usePackaging a project that uses multiple python versionsWhy is permission denied on pip install except for when “--user” is included at end of command?

          Esgonzo ibérico Índice Descrición Distribución Hábitat Ameazas Notas Véxase tamén "Acerca dos nomes dos anfibios e réptiles galegos""Chalcides bedriagai"Chalcides bedriagai en Carrascal, L. M. Salvador, A. (Eds). Enciclopedia virtual de los vertebrados españoles. Museo Nacional de Ciencias Naturales, Madrid. España.Fotos