0

input:

position fst
1 0.6
2 0.8
3 0.9
4 0.3
5 1

This gives me a header:

awk '{if ($2>=0.7) print $1}' input > output

but this doesn't:

awk '{if ($2<0.7) print $1}' input > output

How come?

AWE
  • 4,045
  • 9
  • 33
  • 42
  • I cannot tell you why without your `input`! – kev Jan 11 '12 at 12:14
  • what do you mean by `header`? Can we have a small sample of your `input`? The former command prints the value in the first column of the line if the second column of that line is >= 0.7, and the other prints the value if its < 0.7. So maybe you don't get anything for the second because all values in column two are less than 0.7. – mathematical.coffee Jan 11 '12 at 12:15
  • For me it prints all $1. I think your numbers are being truncated. – Roger Lindsjö Jan 11 '12 at 12:29

2 Answers2

3

In your second example, $2<0.7 is interpreted as "fst"<"0.7" which is FALSE

You can add NR==1 || to always print first line:

$ awk 'NR==1 || $2<0.7{print $1}' input
position
1
4
kev
  • 155,172
  • 47
  • 273
  • 272
1

If you always want to print the header then use:

awk '{if (NR>1) {if ($2>=0.7) print $1} else print $1}'
awk '{if (NR>1) {if ($2<0.7) print $1} else print $1}'
anubhava
  • 761,203
  • 64
  • 569
  • 643