[ad_1]
You can do it easily in awk
, but your numbers seem to be off by 0.02
(see below for reason). For instance, you can do:
awk '
FNR==1 { # if first record
printf "DP\tRatio\n" # output heading
next # skip to next record
}
{
nf = $NF # save copy of last field
gsub (/[,:]/, " ", nf) # replace "," and ":" with " "
split (nf, arr, " ") # split into arr on space
printf "%s\t%.2f\n", arr[4], arr[3]/(arr[2]+arr[3]) # output result
}
' file
Where for example with record 2, arr[3] == 1125
, and arr[2] == 2029
. The result of the computation is 0.36
not 0.34
? (you used 2092
in your calculation instead of the actual 2029
and 553
instead of the actual 533
— mystery solved)
Example Use/Output
You can just paste the script into an xterm with your data file (named file
or whatever you change the filename to) as:
$ awk '
> FNR==1 { # if first record
> printf "DP\tRatio\n" # output heading
> next # skip to next record
> }
> {
> nf = $NF # save copy of last field
> gsub (/[,:]/, " ", nf) # replace "," and ":" with " "
> split (nf, arr, " ") # split into arr on space
> printf "%s\t%.2f\n", arr[4], arr[3]/(arr[2]+arr[3]) # output result
> }
> ' file
DP Ratio
3154 0.36
883 0.40
So the output based on the file with the fields split as shown above is:
DP Ratio
3154 0.36
883 0.40
Creating an Awk Script
Creating a script with awk
to run from the command line is a convenient way to make the awk
commands reusable. For example you can create a file say, splitrec.awk
containing:
#!/bin/awk
FNR == 1 { # if first record
printf "DP\tRatio\n" # output heading
next # skip to next record
}
{
nf = $NF # save copy of last field
gsub (/[,:]/, " ", nf) # replace "," and ":" with " "
split (nf, arr, " ") # split into arr on space
printf "%s\t%.2f\n", arr[4], arr[3]/(arr[2]+arr[3]) # output result
}
Then your use of the script becomes:
$ awk -f splitrec.awk file
DP Ratio
3154 0.36
883 0.40
[ad_2]