remove lines where a field's value is less than or equal to 3 - sed or awk?

49,071

You almost got 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 #1

To specify a range

  • ( ($8 >= -4) && ( $8 <= 4 ) ) 8th field from -4 to 4
  • (NR == 1 ) || ( ($8 >= -4) && ( $8 <= 4 ) ) same, including header
Share:
49,071

Related videos on Youtube

geokrowding
Author by

geokrowding

Updated on September 18, 2022

Comments

  • geokrowding
    geokrowding over 1 year

    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?

  • geokrowding
    geokrowding about 9 years
    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 :)
  • Арсений Черенков
    Арсений Черенков about 9 years
    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
  • Giles
    Giles almost 8 years
    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