Hello everyone. I just started looking at awk to make some things run smoother at work. My programming skills is basic at best so I'm hoping for some compassion and patience.
This is the script I've been working on:
#!/usr/bin/awk -f
{
out1 = "hb_%_occ_" FILENAME;
out2 = "summary_" FILENAME;
gsub (/\(+|\)/," ");
tothb += $10;
tott += $15;
if (NF>=15) {printf "%10.2f %10.1f\n", $10, $15 > out1}
# denom = NR;
# avg_lt = tott/denom;
}
END {
print "Summary data for hbond analysis" > out2;
printf ("\n") > out2;
printf (" Sum of percent: %10.2f\n", tothb) > out2;
printf ("\n") > out2;
printf (" Sum of lifetimes: %10.2f\n", tott) > out2;
# printf (" Average lifetime: %10.6f\n", avg_lt) > out2;
}
1st, The denom=NR calculates the number of rows in the original in-file. I would like to work with the lines >13 but not include the last line. Because of my lack of knowledge I solved this with NF>=15 instead (input file has 55 rows, first 13 is of no use, last row is of no use). Using NF>=14 generates an extra data line with zeros for each column in the out1 file which will screw up further use of this file.
Onwards, I want to divide the sum of column $15 with the number of rows containing data, each input file will have different number of rows so either X/(NR-14), which I cannot get to work. Renders Error, division by zero... Or 'if (NR>13) {printf ....} - last line > out1; instad of (NF>=15). But my guess is that this will only affect out1 and not following taskt?
2nd, is there a way to get awk to ask the user to input a number which can be used for division late in the script? What I'm looking for would maybe look like:
print "Enter number of templates"
inputted_number = $1
avg_hb = tothb/inputted_number
printf (" Average occupancy: %10.2f\n", avg_hb) > out2;
I hope someone can overlook the fact that I'm a beginner at this and maby give me some feedback. If you want a copy of a input file, please send me an e-mail and I'll send a copy.
Best regards, Gustaf
This is the script I've been working on:
#!/usr/bin/awk -f
{
out1 = "hb_%_occ_" FILENAME;
out2 = "summary_" FILENAME;
gsub (/\(+|\)/," ");
tothb += $10;
tott += $15;
if (NF>=15) {printf "%10.2f %10.1f\n", $10, $15 > out1}
# denom = NR;
# avg_lt = tott/denom;
}
END {
print "Summary data for hbond analysis" > out2;
printf ("\n") > out2;
printf (" Sum of percent: %10.2f\n", tothb) > out2;
printf ("\n") > out2;
printf (" Sum of lifetimes: %10.2f\n", tott) > out2;
# printf (" Average lifetime: %10.6f\n", avg_lt) > out2;
}
1st, The denom=NR calculates the number of rows in the original in-file. I would like to work with the lines >13 but not include the last line. Because of my lack of knowledge I solved this with NF>=15 instead (input file has 55 rows, first 13 is of no use, last row is of no use). Using NF>=14 generates an extra data line with zeros for each column in the out1 file which will screw up further use of this file.
Onwards, I want to divide the sum of column $15 with the number of rows containing data, each input file will have different number of rows so either X/(NR-14), which I cannot get to work. Renders Error, division by zero... Or 'if (NR>13) {printf ....} - last line > out1; instad of (NF>=15). But my guess is that this will only affect out1 and not following taskt?
2nd, is there a way to get awk to ask the user to input a number which can be used for division late in the script? What I'm looking for would maybe look like:
print "Enter number of templates"
inputted_number = $1
avg_hb = tothb/inputted_number
printf (" Average occupancy: %10.2f\n", avg_hb) > out2;
I hope someone can overlook the fact that I'm a beginner at this and maby give me some feedback. If you want a copy of a input file, please send me an e-mail and I'll send a copy.
Best regards, Gustaf