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

awk regex calculation for certain range only 1

Status
Not open for further replies.

1in10

Technical User
Jun 29, 2014
8
BR
Once again I need a hint for awk. My aim is to do two calculations on a specific part of a field.
But for the beginning I'll start with one calculation.
If this regex prints all the lines to stdout
Code:
awk '$3 ~ /^June$/' datafile.txt
my task is to fetch the maximum and average for that specific part.
My first attempt gives me all the lines for that month, but I need only the result for that month, beside that it prints to stdout a last line with the name of the matched regex.
Code:
awk '$3 ~ /^June$/; {if ($2>max) max=$2}  END { print max};' datafile.txt
How to make that calculation for only the range of the regex $3 ~ /^June$/; and print just the result without all the other lines?

Can anybody help? Thanks in advance.
 
awk '$3~/^June$/{if($2>max)max=$2}END{print max}' datafile.txt

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
And for the average:
Code:
awk '$3~/^June$/{if($2>max)max=$2;++n;s+=$2}END{print max,(s/n)}' datafile.txt

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
YES, it helps, and this is just one more reason for me using awk and joining here. Great relief indeed, thanks a lot. A tiny semicolon ;-)[thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top