Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I use inputed data as variables and NR in calculations? 1

Status
Not open for further replies.

GusGrave

Programmer
Nov 17, 2010
41
SE
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
 
...
if (NF>=15) {[!]++denom;[/!]printf "%10.2f %10.1f\n", $10, $15 > out1}
...
printf (" Average lifetime: %10.6f\n", tott/denom) > out2;

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you very much PH, it solved problem 1 directly. It's hard when you're just beginning scripting since I cant formulate the logical string, but now when I see it, the solution is fairly obvious.

Again, thank you very much!

So, 1st problem is solved! Any and every input on the second part off the problem as more than welcome!

Can I get the script to ask for a number to be inputted and then use that number as a denominator later in the script?

Personally, it seems that the easiest way to solve this is manually inputing the denominator as a numerical value in the script, but others at my workplace will utilize this script and its not given that everyone can do this!

Best regards
Gustaf
 
BEGIN{t="/dev/tty";printf "Enter number of templates ">t;getline<t;inputted_number=$1}
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
problem 2 solved!

Thank you very, VERY much PHV. now it works like a charm!

Now that one I wouldn't have solved in any foreseeable future! On behalf of everyone in my research group, thank you. You've just made life a lot easier for all of us when handling analysis data!!!
 
Since I already have you here, and feel free to ignore this since it's not the correct forum way to post further questions.

I saw that awk has sqrt function build in, so I thought I'd throw it out there, can I get awk to also calculate standard deviation with something like this example?

Standard Deviation: SD=sqrt((E(x-x_avg)squared)/(n-1))

Code:
printf (" SD lifetime: %10.2f\n", sqrt(($10-(tothb/inputted_number))/denom-1)

Hope you see where I'm getting at, I need to subtract (tothb/inputted_number) from every value in $10 and sum up the results, divide this value with "denom" value minus 1 and draw the sqrt from the result.

Should I perhaps put the ($10-(tothb/inputted_number) separately (as a separate calculation) and include the result from this in the formula?

Best regards
Gustaf




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top