How to extract only values greater than a threshold from a file?
With awk
awk -F: '{if($2>10)print$2}' <filename
Explanations
-
-F:
– sets theF
ield separator to:
-
{if($2>10)print$2}
– for each line, test whether the2
nd field is>10
, if soprint
it -
<filename
– let the shell open filefilename
, that's better than lettingawk
do that, see Stéphane Chazelas' answer on the topic
Example run
$ <filename awk -F: '{if($2>10)print$2}'
15.02
12.58
It'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
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':' -f2
as with sed:
sed -n 's/.*:[^0-9]*\([1-9][0-9][0-9]*\..*\)/\1/p' file
But using RegEx on ordered data is error prone (in my experience) and difficult to read ;-).