简体   繁体   中英

Shell Scripting and use of gawk along with arithmetic operations

I have a tab delimited file and I want to perform some mathematical calculations on the columns present in file.

let the file name be sndf and $tag have some integer value, I want to first find difference between the values of column 3 and 2 then divide column 4 value with the value in $tag again divide the resultant with the difference in values of column 3 and 2 and final result is multiplied by 100.

cat $sndf | gawk '{for (i = 1; i <= NF; i += 1) {
    printf "%f\t"  $3 -$2 "\t",  (((($4/"'$tag'")/($3-$2)))*100);
} printf "\n"}'>normal_wrt_region

the command is writing answer 4 times instead of one time to the output file..... can u all suggest an improvement? thank you

SOLUTION: Dear all, I have solved the problem, thank you all for reading the problem and investing your time.

the command is writing answer 4 times instead of one time to the output file, can u all suggest an improvement?

Don't use the for loop if you don't need one?

cat $sndf | gawk '{ printf "%f\t"  $3 -$2 "\t", (((($4/"'$tag'")/($3-$2)))*100) }'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM