Filter by:

Append string to a file

This awk code snippet append a string to a file. For replacing captured group from a pattern, you may use '\\' + group number.
Copy Code
awk '{ r = gensub(/regex patter/,replace pattern,"g"); print r; }' filename

awk '


r = gensub(/regex pattern/, replace pattern, "g");

print r;



In this script, we use gensub to find a pattern in a file and replace it with another pattern. You may use parantheses in the regex expression to create group. To access the matched group, you use \\N in the replace pattern. Here, N represent the index of the captured group (starts from 1).

Find sum, average and standard deviation of a file

This code snippet finds sum, average and standard deviation (std) of each column of a given file and prints them out to the standard output.
Copy Code
awk '{for(i=1;i<=NF;i++) {sum[i] += $i; sumsq[i] += ($i)^2}} END {printf "sum: " ;for (i=1;i<=NF;i++) {printf "%f\t", sum[i]} print "" printf "avg: " ;for (i=1;i<=NF;i++) {printf "%f\t", sum[i]/NR} print "" printf "std: " ;for (i=1;i<=NF;i++) {printf "%f\t", sqrt((sumsq[i]-sum[i]^2/NR)/NR)} print "" }'

awk '{for(i=1;i<=NF;i++) {sum[i] += $i; sumsq[i] += ($i)^2}}

END {printf "sum: " ;

for (i=1;i<=NF;i++) {printf "%f\t", sum[i]}

printf "\navg: ";

for (i=1;i<=NF;i++) {printf "%f\t", sum[i]/NR}

printf "\nstd: " ;

for (i=1;i<=NF;i++) {printf "%f\t", sqrt((sumsq[i]-sum[i]^2/NR)/NR)}

print ""


The first loop calculates the sum and the squared sum of each column of the given file. The END rule takes care of printing the sum, average, and standard deviation of each column.

Last Update: