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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ignore certain rows and find average of other's colums 3

Status
Not open for further replies.

chlfc

Programmer
Mar 21, 2008
6
Hey guys, I'm new to awk, basically I have an input file, two columns, first I want to ignore rows with the first column that start with a certain string, and with the rest, I want to find the average. How would I do this?

e.g

8113442352 56
8325423535 99
8114525445 80
4526693345 97
4526426535 87

e.g. I want to ignore the rows that start with 811, and find the average of the second column of the others only (the sum of all the others then divide by the number of rows that it added up)

thanks
 
Like this one-liner maby?
Code:
awk '!/^811/ {sum+=$2} END {print sum/NR}' inputfile.txt
 
the number of rows do not include the ones with 811 at the beginning right?
 
awk '!/^811/{t+=$2;++n}END{print t"/"n"="t/n}' /path/to/input

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
NR is the total of all lines in the file.
PHV's solution is correct, mine is NOT!
A star to him.
 
hey guys when i try to run the script, it just shows a blank line, I have to press ctrl+c to get back to the prompt
 
I tried running the script using the awk -f <source> <input1file> <input2file>

and it gives me an invalid char error pointing to the ' after the awk in the scipt
 
wish you could edit old posts. Anyway, I got it to run at least, using the #!/bin/bash then using a $@ at the end after the last quote. However, now it says
Code:
 (FILENAME=<input2> FNR<last line of second input>)  fatal: division by zero attempted
 
i feel stupid, figured it out. turns out i was dividing somethig, put an equal sign and then put the division equation again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top