Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculating the sum of negative numbers in a column?
    text
    copied!<p>Hi I have a file like this:</p> <pre><code>probeset_id submitted_id chr snp_pos alleleA alleleB 562_201 562_202 562_203 562_204 562_205 562_206 562_207 562_208 562_209 562_210 AX-75448119 Chr1_41908741 1 41908741 T C 0 -1 0 -1 0 0 0 0 0 -1 AX-75448118 Chr1_41908545 1 41908545 T A 2 -1 2 2 2 -1 -1 2 2 0 AX-75448118 Chr1_41908545 1 41908545 T A 1 2 2 2 2 -1 2 2 2 0 </code></pre> <p>And I want to calculate the sum of <strong>negative</strong> numbers form column 7th up to end (in real file I have 102 column):</p> <p>I use <code>awk</code> for calculating the normal sum (and it works perfectly):</p> <pre><code>awk 'NR&gt;1 {x+=$8}END{print x}' test.txt </code></pre> <p>but what I want to do is to calculate the sum of <strong>negative numbers only</strong>. and if there is no nagative I would like to have <code>0</code>. so I would like to find an awk command that I run for each column and I get an out put of <code>0</code> or a negative number e.g <code>-1 -2 -3</code> and so on.</p> <p>this is what I was trying:</p> <pre><code>awk '/^-/ {n++;x+=$9} END {print x}' test.txt awk '/^-/ {x+=$9} END {print x}' test.txt awk 'NR&gt;1' test.txt | awk '/^-/ {x+=$9} END {print x}' </code></pre> <p>but I get nothing! no error and now answer! I'm also trying to get an ideal output format:</p> <pre><code>562_201 562_202 562_203 562_204 562_205 562_206 562_207 562_208 562_209 562_210 0 -2 0 -1 0 -2 -1 0 0 -1 </code></pre> <p>I tried several thing which I think are wrong basically:</p> <pre><code>awk 'NR&gt;1 {for ($i=7;i&lt;=NF;$i++) if (x ~ /^-/) x+=$i }END{print x}' test.txt </code></pre> <p>result: my computer swaps!</p> <pre><code>awk 'NR&gt;1 {for (i=7;i&lt;=NF;i++) if ($i ~ /^-/) x+=$i }END{print x}' test.txt </code></pre> <p>result: I get an irrelevant answer(the total sum of negative number in the file)!</p> <p>But this is not really important. I just want the <code>awk</code> command to calculate the sum of <strong>negative numbers</strong> in each column!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload