Note that there are some explanatory texts on larger screens.

plurals
  1. POBash/Nawk whitespace problems
    primarykey
    data
    text
    <p>I have 100 datafiles, each with 1000 rows, and they all look something like this:</p> <pre><code>0 0 0 0 1 0 1 0 2 0 1 -1 3 0 1 -2 4 1 1 -2 5 1 1 -3 6 1 0 -3 7 2 0 -3 8 2 0 -4 9 3 0 -4 10 4 0 -4 . . . 999 1 47 -21 1000 2 47 -21 </code></pre> <p>I have developed a script which is supposed to take the square of each value in columns 2,3,4, and then sum and square root them. Like so:</p> <pre><code>temp = ($t1*$t1) + ($t2*$t2) + ($t3*$t3) calc = $calc + sqrt ($temp) </code></pre> <p>It then calculates the square of that value, and averages these numbers over every data-file to output the average "calc" for each row and average "fluc" for each row.</p> <p>The meaning of these numbers is this: The first number is the step number, the next three are coordinates on the x, y and z axis respectively. I am trying to find the distance the "steps" have taken me from the origin, this is calculated with the formula <code>r = sqrt(x^2 + y^2 + z^2)</code>. Next I need the fluctuation of r, which is calculated as <code>f = r^4</code> or <code>f = (r^2)^2</code>. These must be averages over the 100 data files, which leads me to:</p> <pre><code>r = r + sqrt(x^2 + y^2 + z^2) avg = r/s </code></pre> <p>and similarly for f where s is the number of read data files which I figure out using <code>sum=$(ls -l *.data | wc -l)</code>. Finally, my last calculation is the deviation between the expected <code>r</code> and the average <code>r</code>, which is calculated as <code>stddev = sqrt(fluc - (r^2)^2)</code> outside of the loop using final values.</p> <p>The script I created is: </p> <pre><code>#!/bin/bash sum=$(ls -l *.data | wc -l) paste -d"\t" *.data | nawk -v s="$sum" '{ for(i=0;i&lt;=s-1;i++) { t1 = 2+(i*4) t2 = 3+(i*4) t3 = 4+(i*4) temp = ($t1*$t1) + ($t2*$t2) + ($t3*$t3) calc = $calc + sqrt ($temp) fluc = $fluc + ($calc*$calc) } stddev = sqrt(($calc^2) - ($fluc)) print $1" "calc/s" "fluc/s" "stddev temp=0 calc=0 stddev=0 }' </code></pre> <p>Unfortunately, part way through I receive an error: </p> <pre><code>nawk: cmd. line:9: (FILENAME=- FNR=3) fatal: attempt to access field -1 </code></pre> <p>I am not experienced enough with awk to be able to figure out exactly where I am going wrong, could someone point me in the right direction or give me a better script?</p> <p>The expected output is one file with:</p> <pre><code>0 0 0 0 1 (calc for all 1's) (fluc for all 1's) (stddev for all 1's) 2 (calc for all 2's) (fluc for all 2's) (stddev for all 2's) . . . </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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