I have a column that I would like to subset for a specific value (let's say >= 2) and after dividing by the total amount of the initial number of the column. How can I do that?

Example for a subset >= 2:

Input: a column like this




I have tried with awk something like:

awk '($1 > 2) / $1' myfile

But this is not working.

2 Answers 2


None of the values in your example are > 2 so I will assume you mean >= 2.

awk '$1 >= 2 { t++ } END { print t/NR }' myfile

This will run through each value in column one, if that value is greater than or equal to 2 we will increment the t variable. At the end t will be divided by the total number of records (number of lines) and the result is printed.

If you want it to literally print the equation as well you can do:

awk '$1 >= 2 { t ++ } END { print t"/"NR"="t/NR }' myfile

We can use the dc utility to perform the calculations :

$ < myfile  tr -s ' ' '\t' | cut -f1 |
 dc -e "
   [lM lN / p q]sq
   [lM 1 + sM]sa
   [? z0=q lN 1 + sN d2!>a c z0=?]s? 
   4k 0sN l?x



Brief Explanation:

° Register `N` holds line count.
° Register `M` holds num of lines >= 2.
° Register `q` performs the division, printing it, and quitting. Kinda like the `END` clause of `awk`.
° Register `a` increments the current value stored in register `M`.
° Register `? ` reads the next line from stdin, checks whether it is empty. In case it us then it initiates the end procedure by invoking the q register. Otw, increments register N  the one keeping the line count. Then compares the current line is greater than or equal to 2 . Increments reg M if it is. Then calls itself recursively to redo the same set of operations on the next line.
° 4k will set output accuracy to four digits and 0sN shall initialize the line counter, l?x will set the ball rolling recursively. 
  • where do you define >= 2 or what so ever threshold here? Jul 28, 2019 at 15:03
  • 1
    The d2!>a clause will execute the contents of reg a when 2 is not greater than the column value IOW, column value is > 1 or you can say >= 2. Jul 28, 2019 at 18:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .