Smart questions
Smart people
 Find A ForumFind An Expert
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Remember Me

Are you a
Computer / IT professional?
Join Tek-Tips now!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Just copy and paste the

Feedback

"...A lot of the information I've found at this site would've taken me forever if I'd have attempted to research it on my own. Thanks again."

Geography

Where in the world do Tek-Tips members come from?

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

 Forum Search FAQs Links Jobs Whitepapers MVPs
 GusGrave (Programmer) 17 Nov 10 9:01
 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 = \$1avg_hb = tothb/inputted_numberprintf ("   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
 PHV (MIS) 17 Nov 10 9:12
 ...if (NF>=15) {++denom;printf "%10.2f %10.1f\n", \$10, \$15 > out1}...printf ("   Average lifetime:    %10.6f\n", tott/denom) > out2;
 GusGrave (Programmer) 17 Nov 10 9:38
 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 regardsGustaf
 PHV (MIS) 17 Nov 10 9:51
 BEGIN{t="/dev/tty";printf "Enter number of templates ">t;getline
 GusGrave (Programmer) 17 Nov 10 10:01
 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!!!
 GusGrave (Programmer) 17 Nov 10 10:40
 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 regardsGustaf

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!