awk (gawk) was compiled with GNU MPFR and MP Bignum libraries:awk --version0 0.0165 10 0.0124 20 0.0093 30 0.0071 40 0.0053 50 0.0039 60 0.0029
awk '{ sum += $1} ; END {print sum}' N2O5.txt awk :
awk -F' ' '{print $1, $2, log($2), 1/($2)}' N2O5.txt
awk -F' ' '{natlog=log($2); inv=1/$2; print $1, $2, natlog, inv}' N2O5.txt
awk -F' ' '{natlog=log($2); inv=1/$2; printf "%2d %.8f %.8f %.8f\n", $1, $2, natlog, inv}' N2O5.txt
awk 'BEGIN { printf "%.17f\n", 99.15-20.85 }'
awk -M -v PREC="double" 'BEGIN { printf "%.17f\n", 99.15-20.85 }'
awk -M -v PREC="quad" 'BEGIN { printf "%.17f\n", 99.15-20.85 }'
awk -M -v PREC=113 'BEGIN { printf "%.17f\n", 99.15-20.85 }'
awk loops to calculate $y_2-y_1$, $y_3-y_2$, $y_4-y_3$ ... etc, or divisions alike.
awk -F' ' '{printf " " $2}' N2O5.txt | awk -F' ' '{for (i=1;i<NF;++i) print $(i+1)"-"$i}' | bc -l
awk -F' ' '{printf " " $2}' N2O5.txt | awk -F' ' '{for (i=1;i<NF;++i) printf $(i+1)-$i"\n"}'
awk -F' ' '{printf " " $2}' N2O5.txt | awk -F' ' '{for (i=1;i<NF;++i) printf "%.8f\n", $(i+1)-$i}'
awk -F' ' '{printf " " $2}' N2O5.txt | awk -F' ' '{for (i=1;i<NF;++i) print "scale=8; "$(i+1)"/"$i}' | bc -l
awk :
awk '{if (NR>1) printf "%3d\t %s\t %.8f\n", $1, $2, ($3-y); y=$3 }' ~jsyu/atom.3 |grep -B1 -
awk '{if (NR>1) printf "%.8f\n", ($2-y)/($1-x); x=$1; y=$2 }' N2O5.txt
awk '{if (NR>1) print "("$2"-y)/("$1"-x)"; x=$1; y=$2 }' N2O5.txt | bc -l
↑↑↑ The loop will NOT work outside awk!!
awk, script driven by bash
#!/bin/bash
exec awk '
BEGIN { FS = "[ ,\t]+" }
NF == 2 { x_sum += $1
y_sum += $2
xy_sum += $1*$2
x2_sum += $1*$1
num += 1
x[NR] = $1
y[NR] = $2
}
END { mean_x = x_sum / num
mean_y = y_sum / num
mean_xy = xy_sum / num
mean_x2 = x2_sum / num
slope = (mean_xy - (mean_x*mean_y)) / (mean_x2 - (mean_x*mean_x))
inter = mean_y - slope * mean_x
for (i = num; i > 0; i--) {
ss_total += (y[i] - mean_y)**2
ss_residual += (y[i] - (slope * x[i] + inter))**2
}
r2 = 1 - (ss_residual / ss_total)
printf("Slope : %g\n", slope)
printf("Intercept : %g\n", inter)
printf("R-Squared : %g\n", r2)
}'
|
linereg_awk.bash then chmod +x linereg_awk.bash and run it by:./linereg_awk.bash < N2O5.txt
awk :transpose.bash
#!/bin/bash
# Transpose the row and column for a matrix.
# Written by Geoff Clare
# Published in "sed & awk" O'Reilly, page 432, ISBN:9781565922259 (Mandarin Chinese version).
exec awk '
NR == 1 {
n = NF
for ( i=1 ; i <= NF ; i++ )
row[i] = $i
next
}
{
if ( NF > n )
n = NF
for ( i=1 ; i <= NF ; i++ )
row[i] = row[i] " " $i
}
END {
for ( i=1 ; i <= NF ; i++ )
print row[i]
}' ${1+"$@"}
|
matrix.txt containing an 8×8 matrix:./transpose.bash < matrix.txt gives: