Home > Net >  Three awk calls in 1
Three awk calls in 1

Time:12-02

The following works as expected:

awk '$1 <= -700 { print $3 }' FS="," tmp | awk '!seen[$0]  '

23
60
73
91

and now I count those four values and print the number 4:

awk '$1 <= -700 { print $3 }' FS="," tmp | awk '!seen[$0]  ' | awk '{ count   } END { print count }'

4

Is there a shorter way to do these three awk calls in one call?

Hints are much appreciated,

CodePudding user response:

Like this:

awk '$1 <= -700 && !seen[$3]   {c  } END{print c 0}' FS="," tmp

Explanation:

# If column 1 <= -700 and we've not seen the value of column 3 yet ...
$1 <= -700 && !seen[$3]   {
   # ... increment the counter c
   c  
}

# When the end of the input file is reached, print the counter
END {
    # Note: incrementing the counter by 0 ensures that c
    # has the value 0 when no line matched the criterias and thereby
    # c has never been incremented. Without this, c would be an
    # empty string. This gets often forgotten. Thanks @Ed Morton!

    # Alternatively you may run the program as awk -v c=0 ...
    print c 0 
}

CodePudding user response:

Count values? Just put the values in the array and print the length, you do not need to print anything.

awk '$1 <= -700 { uniq[$3] } END{ print length(uniq) }'
  • Related