14

I need to remove every line that has a value of 2 or less in the 8th field (column).

My data looks like this:

12-31   Airport 189 379 41  49.70946503 -124.91377258   2   2880    30.8
01-01   AlberniElementary   165 331 16  49.26100922 -124.80662537   4   5760    26.1
01-09   BamfieldMarine  161 323 23  48.83490372 -125.13572693   2   2875    27.4
01-10   BamfieldMarine  161 323 23  48.83490372 -125.13572693   3   3068    38.6

I understand that using awk I can strip off the values desired and print them to another file, and I understand that sed would edit the current file. In either case, I need to retain the original file.

Note: Please provide thorough explanations with your solutions. It is not suffice to just write the command, please annotate suggestions.

Further note: The data has a header line, so most likely solution will need to

awk 'FNR >1'

I suppose?

17

you almost make it

 awk '(NR>1) && ($8 > 2 ) ' foo > bar

where

  • NR is number of record (that is number of line)
  • $8 is eight field
  • && is logical and
  • foo is the original file, unchanged
  • bar resulting file
  • implicit default action is to print the current input line

note that header is striped from foo to bar, to keep it

 awk '(NR==1) || ($8 > 2 ) ' foo > bar

where

  • || is logical or
  • input line is printed if NR==1 or if $8 > 2

update

to specify a range

  • ( ($8 >= -4) && ( $8 <= 4 ) ) 8th field from -4 to 4
  • (NR == 1 ) || ( ($8 >= -4) && ( $8 <= 4 ) ) same, including header.
  • Great answer: simple yet thorough, thanks. Just so i'm clear, the difference between FNR and NR in this case is nothing, correct? I have read the man page where it explains: NR ordinal number of the current record & FNR ordinal number of the current record in the current file. So I understand these being equal in this case, I think :) – geokrowding Mar 5 '15 at 10:34
  • If you have one file, FNR is always NR, if file1 has 10 lines, first line of file2 would have NR=11 and FNR=1 – Archemar Mar 5 '15 at 10:45
  • Hi I want to do something similar but accept everything in a range of -4 to 4. how would I go about doing this, as this is the only way I can think of doing it currently awk '(NR==1) || ($8 = [-4-4] ) ' foo > bar – Giles Jul 15 '16 at 13:24
  • 1
    awk doesn't know about math style range, see my edit. – Archemar Jul 15 '16 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.