Rick
Rick

Reputation: 67

keeping header lines that begin with # using awk

I have a number of files I want to process. For each one, I want to remove any rows where the value in the 6th column is less than 50. I tried using: awk '($6 >= 50)'. However, this removes the header lines from the files, which I want to keep.

Each line of the header starts with a '#' symbol. So I need to modify the command so that it doesn't remove any lines that start with a #.

Upvotes: 0

Views: 262

Answers (2)

Jens
Jens

Reputation: 72649

This sounds like the first pattern/action statement in your script should be

/^#/ { print; next }

The next statement causes awk to skip the remaining pattern/action combos and continue with the next line.

Upvotes: 0

fedorqui
fedorqui

Reputation: 289725

Just say

awk '$1 ~ /^#/ || ($6>=50)' file

This tells awk to print lines that match either of these:

  • First field starts with #. This way, leading spaces are overlooked, or
  • 6th field is bigger or equal of 50.

Little test:

$ cat a
a1 2 3 4 5 52
a2 2 3 4 5 52
#a3 2 3 4 5 12
  #a4 2 3 4 5 12
a5 2 3 4 5 12
$ awk '$1 ~ /^#/ || ($6>=50)' a
a1 2 3 4 5 52
a2 2 3 4 5 52
#a3 2 3 4 5 12
  #a4 2 3 4 5 12

Upvotes: 2

Related Questions