P { margin-bottom: 0.25cm; line-height: 120%; }CODE.cjk { font-family: "WenQuanYi Micro Hei",monospace; }CODE.ctl { font-family: "Lohit Hindi",monospace; }A:link { } I'm trying to build an awk statement to print from a file (file1):
A 1,2,3 *
A 4,5,6 **
B 1
B 4,5 *
Another file like this:
A 1,2,3 * 3 1 0.333
A 4,5,6 ** 3 2 0.666
B 1 1 0 0
B 4,5 * 2 1 0.5
In this new file, the first three columns are the same as in the original file. The forth column must contain the number of comma separated elements in column 2. The fifth column must contain the number of characters in column 3. The last column contains the proportion of column 5 on column 4.
I'm trying the following code:
awk -F ',' '{print $1"\t"$2"\t"$3"\t"NF-1"\t"length($3)"\t"(length($3)/ NF-1)}' file1 > file2
But the output is unexpected (it seems that the second column is splitted, and thus all the calculations are wrong).
~$ cat file2
A 1 2 3 * 2 4 0.333333
A 4 5 6 ** 2 5 0.666667
B 1 0 0 -1
B 4 5 * 1 0 -1
Thank you for your help.
EDIT
P { margin-bottom: 0.25cm; line-height: 120%; }CODE.cjk { font-family: "WenQuanYi Micro Hei",monospace; }CODE.ctl { font-family: "Lohit Hindi",monospace; }A:link { } I actually fixed one of my errors, but I still have no idea why the fourth column doesn't print correctly. Here's my new code and output:
awk '{print $1"\t"$2"\t"$3"\t"(NF","$2 -1)"\t"length($3)"\t"(length($3)/(NF","$2-1))}' file1 > file2
:~$ cat file2
A 1,2,3 * 3,0 1 0.333333
A 4,5,6 ** 3,3 2 0.666667
B 1 2,0 0 0
B 4,5 * 3,3 1 0.333333