Reputation: 527
I have a code:
/Curve No./ { in_f_format=1; next }
/^[[:space:]]*$/ { in_f_format=0; next }
{sum2+=$2; sum3+=$3} END{printf("%.6f\n",sum3/sum2)}
That find a table in text file and provide a computation. The result is one number from one file. How to get a column of results for more files. I wrote:
awk -f program.awk file??.txt
and I get only one result - for file01.txt
Input file01
Curve No. of obs. Sum of squares
1 82 0.81604656
2 7200 96272.93714063
3 7443 110384.79793831
jkjl
Input file02
Curve No. of obs. Sum of squares
1 82 0.81604656
2 7200 96272.93714063
3 7443 110384.79793831
jkjl
Desired input - two numbers in case of two input files.
14.034536
14.034536
Upvotes: 1
Views: 94
Reputation: 37394
awk '
FNR==1{argind++} # if you are on GNU awk, remove this and...
/Curve No./ { in_f_format=1; next }
/^[[:space:]]*$/ { in_f_format=0; next }
{sum2[argind]+=$2; sum3[argind]+=$3} # ... replace argind with ARGIND here
END{
for(i=1;i<=argind;i++) # ... and here.
printf("%.6f\n",sum3[i]/sum2[i])
}' file1 file2
Output:
14.034537
14.034537
If you are using GNU awk, you can replace argind
with builtin ARGIND
and remove the FNR==1
block.
Update:
If you are on GNU awk you could use theENDFILE
:
$ awk '/Curve No./ { in_f_format=1; next }
/^[[:space:]]*$/ { in_f_format=0; next }
{sum2+=$2; sum3+=$3}
ENDFILE { # ENDFILE after every file
printf("%.6f\n",sum3/sum2) # print
sum3=sum2=0 # reset vars
}' file file # all those files
14.034537
14.034537
Another update:
Or you could move the printf
and remove the END
block:
$ awk '/Curve No./ { in_f_format=1; next }
/^[[:space:]]*$/ {
in_f_format=0
printf("%.6f\n",sum3/sum2) # move it here
sum2=sum3=0 # reset vars
next
}
{sum2+=$2; sum3+=$3}' file file
14.034537
14.034537
Upvotes: 4