- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- ave and max
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2007 05:54 PM
02-13-2007 05:54 PM
I have a file which contains numbers (some of them decimals). For each file has different word count (wc -l). I computed for each the average and maximum value. I did use for loops like for i in `cat file`
do
x = `expr $x + i | bc -l`
done
x is at initial zero then I use it as well by checking max by "if" logic.
But it requires time so I may ask if you have any idea how can I use awk in it for the average and maximum values in that file.
Will appreciate your help. Regards....
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2007 07:53 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2007 09:49 PM
02-13-2007 09:49 PM
Re: ave and max
assuming one value per line:
(/> is my prompt)
/> cat f1
1.5
2
4
0.5
0
To get what you need:
/> sort -nk1 f1|awk '{t+=$1};END {print "Total=" t " Lines=" NR " AVG=" t/NR " MAX=" $1}';
Total=8 Lines=5 AVG=1.6 MAX=4
I print Total and Lines for debug, but you can avoid this.
HTH,
Art
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2007 05:17 AM
02-14-2007 05:17 AM
Re: ave and max
I don't understand what you mean by
>>
For each file has different word count (wc -l)
<<
Do you have to parse several files, all containing numbers you want to do calculation with? You can use the awk internal variables 'FILENAME' and 'FNR' for this.
Next: is there only one number per line or do you have multiple entries per line? You would have to
- loop over all (valid?) fields of line in that case (e.g. for(i=1;i<=NF;i++) ...).
- add the number of fields instead of using FNR (e.g. num+=NF)
in that case.
awk 'BEGIN {max="undef"}
FILENAME && FILENAME != lastf {printf("max=%s avg=%s\n", max,sum/FNR); max="undef"; sum=0 }
{lastf=FILENAME; sum+=$1; if(max=="undef") max=$1; else if($1>max) max=$1 }
END {printf("max=%s avg=%s\n", max,sum/FNR)}' file1 file2 ...
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2007 08:29 PM
02-14-2007 08:29 PM
Re: ave and max
I assumed that the script would have to calculate the number of records and couldn't be a constant.