awk related question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blessedgirl
    New Member
    • Jan 2010
    • 3

    awk related question

    hey sir,

    i have a question regarding how to sum and get average of records of files using awk programming

    i have several output files like this

    file 1
    year col1 col2 col3 col4
    1 0.4 0.1 0.3 0.8
    1 0.3 0.2 0.9 2
    2 0.1 0.3 0.8 0.7
    2 0.2 0.4 0.9 0.7
    3 0.4 0.3 0.8 0.3
    3 0.2 0.4 0.4 0.2
    4 0.8 0.4 0.8 0.3
    4 0.5 0.4 0.5 0.5

    file 2
    year col1 col2 col3 col4;
    1 0.4 0.1 0.3 0.8
    1 0.3 0.2 0.9 2
    2 0.1 0.3 0.8 0.7
    2 0.2 0.4 0.9 0.7
    3 0.4 0.3 0.8 0.3
    3 0.2 0.4 0.4 0.2
    4 0.8 0.4 0.8 0.3
    4 0.5 0.4 0.5 0.5

    file 3
    year col1 col2 col3 col4
    1 0.4 0.1 0.3 0.8
    1 0.3 0.2 0.9 2
    2 0.1 0.3 0.8 0.7
    2 0.2 0.4 0.9 0.7
    3 0.4 0.3 0.8 0.3
    3 0.2 0.4 0.4 0.2
    4 0.8 0.4 0.8 0.3
    4 0.5 0.4 0.5 0.5

    and i want the average out puts of all the records of a files to be summed and averaged in to one file and looks like this

    year col1 col2 col3 col4
    1
    1
    2
    2
    3
    3
    4
    4
    5
    5
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    I am moving this to the Unix forum as this is the Perl forum. Please choose the best appropriate forum next time.

    Regards,

    Jeff

    Comment

    • ashitpro
      Recognized Expert Contributor
      • Aug 2007
      • 542

      #3
      Code:
      #!/bin/bash
      file1=$1
      file2=$2
      file3=$3
      
      total_lines=`cat $file1 |wc -l`
      num_lines=`expr $total_lines - 1`
      for (( j=2; $j <= $num_lines; j++ ))
      do
              l_file1=`head -n $j $file1 | tail -1`
              l_file2=`head -n $j $file2 | tail -1`
              l_file3=`head -n $j $file3 | tail -1`
      
              echo $l_file1 > /tmp/tmp.txt
              echo $l_file2 >> /tmp/tmp.txt
              echo $l_file3 >> /tmp/tmp.txt
      
              year_num=`echo $l_file1 | awk {'print $1'}`
      
              col1_sum=`awk '{s+=$2}END{print s}' /tmp/tmp.txt`
              col1_avg=`calc $col1_sum / 4`
      
              col2_sum=`awk '{s+=$3}END{print s}' /tmp/tmp.txt`
              col2_avg=`calc $col2_sum / 4`
      
              col3_sum=`awk '{s+=$4}END{print s}' /tmp/tmp.txt`
              col3_avg=`calc $col3_sum / 4`
      
              col4_sum=`awk '{s+=$5}END{print s}' /tmp/tmp.txt`
              col4_avg=`calc $col4_sum / 4`
      
              echo $year_num $col1_avg $col2_avg $col3_avg $col4_avg
      done
      This script accepts three cmd parameters i.e. three file names.
      Its is tested for three files with four columns..
      e.g.
      ./a.sh file1 file2 file2

      You will need calc command to get this script run.
      If it is not install, use following command to install..

      [UBUNTU]
      apt-get install apcalc

      [RED HAT/FEDORA]
      yum install apcalc

      Comment

      Working...