find the Integer value after a string from a file
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 20I want to get all the integer values in sorted order.
03 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
20The -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
200If, instead, you need to anchor your pattern using a specific string, use:
$ grep -oP -- '-weight\s+\K\d+' file | sort -n
20
100
200The \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 -nFor 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 columnsort -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 -nOr
sed -E 's/.*weight\s([0-9]*).*/\1/' sort.txt | sort -nTest case:
-weight 100
-weight 200
-weight 20Results:
20
100
200Note:- 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.