M HYPE SPLASH
// general

find the Integer value after a string from a file

By Emma Valentine

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.

0

3 Answers

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 -- '-weight\s+\K\d+' 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 -- '-weight\s+\K[0-9,-]+' file | sort -n

For example:

$ cat file
-weight 100
-weight 200
-weight 20
-weight -29
-weight -32.4
$ grep -oP -- '-weight\s+\K[0-9,-]+' file | sort -n
-32
-29
20
100
200
2

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
2

Try this sed command:

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

Or

sed -E 's/.*weight\s([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:

  • ^.*weight\s([[: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.
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy