473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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
Jan 21 '10 #1
2 1659
numberwhun
3,509 Expert Mod 2GB
I am moving this to the Unix forum as this is the Perl forum. Please choose the best appropriate forum next time.

Regards,

Jeff
Jan 24 '10 #2
ashitpro
542 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. #!/bin/bash
  2. file1=$1
  3. file2=$2
  4. file3=$3
  5.  
  6. total_lines=`cat $file1 |wc -l`
  7. num_lines=`expr $total_lines - 1`
  8. for (( j=2; $j <= $num_lines; j++ ))
  9. do
  10.         l_file1=`head -n $j $file1 | tail -1`
  11.         l_file2=`head -n $j $file2 | tail -1`
  12.         l_file3=`head -n $j $file3 | tail -1`
  13.  
  14.         echo $l_file1 > /tmp/tmp.txt
  15.         echo $l_file2 >> /tmp/tmp.txt
  16.         echo $l_file3 >> /tmp/tmp.txt
  17.  
  18.         year_num=`echo $l_file1 | awk {'print $1'}`
  19.  
  20.         col1_sum=`awk '{s+=$2}END{print s}' /tmp/tmp.txt`
  21.         col1_avg=`calc $col1_sum / 4`
  22.  
  23.         col2_sum=`awk '{s+=$3}END{print s}' /tmp/tmp.txt`
  24.         col2_avg=`calc $col2_sum / 4`
  25.  
  26.         col3_sum=`awk '{s+=$4}END{print s}' /tmp/tmp.txt`
  27.         col3_avg=`calc $col3_sum / 4`
  28.  
  29.         col4_sum=`awk '{s+=$5}END{print s}' /tmp/tmp.txt`
  30.         col4_avg=`calc $col4_sum / 4`
  31.  
  32.         echo $year_num $col1_avg $col2_avg $col3_avg $col4_avg
  33. done
  34.  
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
Jan 25 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tomba | last post by:
:(( posted a question here 3 days ago, now I am able to come look at answers it's no more available :(( so... @supapablo: if you still have your answer, mail me... tomba at f1technical dot net...
7
by: Maxwell Hammer | last post by:
Hi all, This is related to an earlier post 'Help with thread related tracebacks'...for which I have had no feedback yet :-( How should a thread complete i.e. how should it exit? Reading the...
0
by: QWERTY | last post by:
--------------Boundary-00=_O5I3QL80000000000000 Content-Type: Multipart/Alternative; boundary="------------Boundary-00=_O5I3LVC0000000000000" --------------Boundary-00=_O5I3LVC0000000000000...
17
by: Steve Jorgensen | last post by:
Terminology question: Is there a term for a set of records related directly or indirectly by key value in several tables? For example, a single invoice record and its line item records -or- a...
2
by: Colm O'Hagan | last post by:
Hi there, I having a problem with a database I'm setting up, I would be delighted if someone out there could help. The database I'm setting up is a task register datebase, it will be used to...
7
by: Susan Bricker | last post by:
Greetings. As a relative newcomer to Access, I am having trouble deciding on how to design the form flow for updating and creating related records. I'm looking for a variety of suggestions so...
0
by: archana | last post by:
Hi all, I have some confusion related to threading and autoresetevent. I have 5 threads running for each i have autoresetevent which i am setting through thread procedure. On each of event...
2
by: archana | last post by:
Hi all, I don't have much of knowledge about threading. I have on .net application where in main i am starting one secondary thread. like
0
by: orit | last post by:
Again, I have the following xml code: <?xml version="1.0" ?> <course> <globalProperties> <externalMetadata> <source>ADL</source> <model>ADL SCORM 1.0</model> </externalMetadata>...
4
by: HLCruz via AccessMonster.com | last post by:
I am working with a database that has client information separated in to 4 related tables - tFolder, tAddress, tEmail, tPhone number. In addition there are related tables tGifts and tCalls. The...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.