We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


calculating average of different input files

london_town asked
Medium Priority
Last Modified: 2012-06-27
Hi everyone!

Currently using awk to carry out tasks within a bash script.

I'm having difficulty carrying out tasks with each individual file being read in as a command-line argument. For example, I can calculate the average records from all files but not the average record from each file.  The amount of filenames read in as command-line arguments is unlimited so I have been using $* as the input for my awk script.  

I tried using the following conditions to resolve this problem but was of no consequence:

for (x =1; x < ARGC; x++){
if (ARGV[x] = FILENAME){

Your help would be much appreciated
Watch Question

I'm a bit confused.

You have an awk script to compute the average for a file...

Do you want to do something like:

for i in $*
         average=$(awk-script $i)
         echo for $i the average is $average

Most Valuable Expert 2014
Top Expert 2015
awk 'prev!=FILENAME{if(n){ print prev, total/n; } n=0; total=0; prev=FILENAME}{n++;total+=$1}END{ if(n){ print prev, total/n; } }' $*

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Thanks guys!
Spot on ozo, could not continue my script without this assistance.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.