Find the answer to your Linux question:
Results 1 to 2 of 2
Hi everyone, just some simple question... i've been using a awk script to calculate my data... i have 3 files: file a1.txt: 2 3 4 5 3 4 file a2.txt: ...
  1. #1
    yat
    yat is offline
    Just Joined!
    Join Date
    May 2011
    Posts
    5

    Calculate data and make it into new column using awk

    Hi everyone, just some simple question...

    i've been using a awk script to calculate my data...

    i have 3 files:

    file a1.txt:
    2
    3
    4
    5
    3
    4

    file a2.txt:
    4
    5
    6
    7
    8

    file a3.txt:
    2
    5
    1
    3
    4

    i successfully calculate the average of each file using awk:
    awk '{ s += $1 } END { print s/NR }' a1.txt
    awk '{ s += $1 } END { print s/NR }' a2.txt
    awk '{ s += $1 } END { print s/NR }' a3.txt

    the results were (3.5, 6 and 3) which is pretty easy..

    now i want to combine all this into 1 file and each have different columns and called it avg.txt which have something like this in the end:

    3.5 6 3

    help is much appreciated, Tq

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Code:
    awk 'NR==1{s=$1; f=FILENAME; next}
    f != FILENAME {a=a?a FS s/n:s/n ; f=FILENAME; s=0}
    {s+=$1; n=FNR}
    END{print a, s/n}' a1.txt a2.txt a3.txt

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...