views:

76

answers:

3

I have a file with two columns,

sdfsd 1.3
sdfds 3
sdfsdf 2.1
dsfsdf -1 

if x is 2

I want to print sdfsdf 2.1

How to express it in awk (bash or sed is fine too)

+1  A: 

awk:

BEGIN {
  min=0
  mint=""
  threshold=2
}
{
  if($2 > threshold && ($2 < min || min == 0)) {
    min = $2
    mint = $1
  }
}
END
{
  print mint, min
}
VeeArr
Output is zero if threshold is the same as the max value. It should probably output an error.
Dennis Williamson
A: 
NR==1 { min = $2 }
NR > 1 {
    if ($2 > X){
       if (min > $2){
        min = $2
        text = $1
       }
 }
}
END {print text, min}
Ehsan
contains several errors: literal 2 not used, comparison against uninitialized X...
Norman Ramsey
comparison against uninitialized values in awk is a common shortcut because they are guaranteed to return 0;
SiegeX
+2  A: 

It's awfully tempting to do this:

sort -k 2 -g  | awk '$2 >= 2 { print; exit }'

Tested and works on your example. If no second column is at least 2, it prints nothing.

Norman Ramsey