awk printing lines in different order to original file -


i have csv file 6 columns. col3 id, , col4 count. want print col3, , convert col4 frequency.

col1,col2,col3,col4,col5,col6 9,19,9,7,9,6 10,132,10,131,10,65 10.3,0,10.3,0,10.3,1 11,128,11,182,11,82 

my command

awk -f"," '{if (nr!=1) f[$3] = $4; sum += $4} end { (i in f) { print i, f[i]/sum } }' myfile.csv > myoutfile.txt 

unexpectedly, printing output lines in wrong order - 10.3 comes before 10.
there way fix this

9,0.021875 10.3,0 10,0.409375 11,0.56875 

here 1 way using awk:

awk 'begin{fs=ofs=","}fnr==1{next}nr==fnr{sum+=$4;next}{print $3,(sum>0?$4/sum:0)}' file file 9,0.021875 10,0.409375 10.3,0 11,0.56875 

you 2 passes file. both passes check if first line, skip doing fnr==1{next}. in first pass, create variable sum , keep adding column 4 value it. in second pass print 3rd column along frequency (4th column / sum).

notice have used file file due 2 passes. can use brace expansion , file{,}


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -