How to extract only values greater than a threshold from a file?
By Jessica Wood •
I have this file:
names average
john:15.02
Mark:09.63
James:12.58I want to extract only the averages greater than 10 from it, so the output in this example should be:
15.02
12.58 3 Answers
With awk
awk -F: '{if($2>10)print$2}' <filenameExplanations
-F:– sets theField separator to:{if($2>10)print$2}– for each line, test whether the2nd field is>10, if soprintit<filename– let the shell open filefilename, that's better than lettingawkdo that, see Stéphane Chazelas' answer on the topic
Example run
$ <filename awk -F: '{if($2>10)print$2}'
15.02
12.58It's also possible to add spaces and put the pattern outside the brackets, so these are equal – thanks to Stefan for pointing that out:
awk -F: '{if($2>10)print$2}' <filename
awk -F: '{ if ( $2 > 10 ) print $2 }' <filename
awk -F: '$2>10{print$2}' <filename
awk -F: '$2 > 10 { print $2 }' <filename 4 With grep you'd have to work with regular expressions; e.g.
grep -E ':[^0-9]*[1-9][0-9][0-9]*\.' file | cut -d':' -f2as with sed:
sed -n 's/.*:[^0-9]*\([1-9][0-9][0-9]*\..*\)/\1/p' fileBut using RegEx on ordered data is error prone (in my experience) and difficult to read ;-).
2Im posting also this solution
sed '1d' File|cut -d: -f2 |awk '$0>10' sed '1d' = removes the header
cut -d: -f2 = keeps values after the delimiter (: in this case)
awk '$0>10' = prints all values greater than 10