calculating average of different input files

Posted on 2006-03-25
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
Question by:london_town
    LVL 3

    Expert Comment

    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

    LVL 84

    Accepted Solution

    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; } }' $*

    Author Comment

    Thanks guys!
    Spot on ozo, could not continue my script without this assistance.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
    The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now