Summing data on N. row of each line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Summing data on N. row of each line
# 1  
Old 04-18-2012
Summing data on N. row of each line

Hi friends, lets assume that I have lines of data as follows

rico 12 bct 58
pot 65 vft 41
dek 45 kbt 13
her 35 ubr 14
. . . .
. . . .
. . . .

I want to calculate the sum of the numbers at the 2. row of each line.

Is it possible to do by using a simple code ?

thanks
# 2  
Old 04-18-2012
summary
Code:
# awk '{a+=$2}END{print a}' infile

average
Code:
# awk '{a+=$2;x++}END{print a/x}' infile

This User Gave Thanks to ygemici For This Post:
# 3  
Old 04-18-2012
Quote:
Originally Posted by ygemici
summary
Code:
# awk '{a+=$2}END{print a}' infile

average
Code:
# awk '{a+=$2;x++}END{print a/x}' infile

thank you ygemici for quick response. summation works well. however the code for average counts the empty lines as a data line and the average becomes wrong.
# 4  
Old 04-18-2012
average

Code:
awk '/./{a+=$2;x++}END{print a/x}' infile

This User Gave Thanks to itkamaraj For This Post:
# 5  
Old 04-18-2012
Try like...

Code:
awk -F '|' '{s+=$2} END {print s}' test1.txt

This User Gave Thanks to bmk For This Post:
# 6  
Old 04-18-2012
Quote:
Originally Posted by rpf
thank you ygemici for quick response. summation works well. however the code for average counts the empty lines as a data line and the average becomes wrong.
Code:
# awk '!/^ *$/{a+=$2;x++}END{print a/x}' infile

This User Gave Thanks to ygemici For This Post:
# 7  
Old 04-18-2012
thank you guys both of them works.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Emergency UNIX and Linux Support

[Solved] Mysql - Take data from row and copy it to another row

Sorry if I repost my question in this section, but I'm really in a hurry since I have to finish my work... :( Dear community, I have a table with two rows like: Row1 Row2 ======= ======= 7,3 text 1 1,3 text 2 1,2,3 blabla What i need to do is add/copy... (2 Replies)
Discussion started by: Lord Spectre
2 Replies

2. Shell Programming and Scripting

Summing columns in line

I have a file with the following format AAAAA 1.34B 0.76B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.90B 0.00B 0.00B 0.46B 0.00B 0.03B 0.00B ... (4 Replies)
Discussion started by: ncwxpanther
4 Replies

3. Shell Programming and Scripting

Using awk to summing from a given line

My file is something like this : 03.097 03.094 03.093 03.095 03.091 04.089 06.093 07.225 08.196 06.097 06.094 05.096 04.086 I'd like to sum it from a given line to another one , e.g.: from line 10 until line 20 What s the awk way solving this ? (1 Reply)
Discussion started by: firelink
1 Replies

4. UNIX for Advanced & Expert Users

Convert column data to row data using shell script

Hi, I want to convert a 3-column data to 3-row data using shell script. Any suggestion in this regard is highly appreciated. Thanks. (4 Replies)
Discussion started by: sktkpl
4 Replies

5. Shell Programming and Scripting

Summing up rows data regarding 1st column

Dear all, I have one file like LABEL A B C D E F G H I J K L M N G02100 64651.3 25630.7 8225.21 51238 267324 268005 234001 52410.9 18598.2 10611 10754.7 122535 267170 36631.4 G02100 12030.3 8260.15 8569.91 ... (4 Replies)
Discussion started by: AAWT
4 Replies

6. Shell Programming and Scripting

Reading several files and summing their content line-by-line

Hey, I am gettin a bit crazy with my script. I have several input datas with the same name (5.ill) in different folders (daysim_01, daysim_02, etc.). The 4. column of each of the data has to be summed with each other and then hass to be written in one new file. So file1: 1 1 0 1 2 1 1 2 ... (7 Replies)
Discussion started by: ergy1983
7 Replies

7. Shell Programming and Scripting

Add value of each row when each row has different data ype

Guys -- I am noob and I am really strugging on this one. I cant even write the pseudo code for it. I have a file that spits values in individual rows as such: file: user date type size joe 2005-25-09 log 1 joe 2005-25-09 snd 10 joe 2005-25-09 rcd 12 joe 2005-24-09 log 2 joe... (1 Reply)
Discussion started by: made2last
1 Replies

8. Shell Programming and Scripting

How to insert data befor some field in a row of data depending up on values in row

Hi I need to do some thing like "find and insert before that " in a file which contains many records. This will be clear with the following example. The original data record should be some thing like this 60119827 RTMS_LOCATION_CDR INSTANT_POSITION_QUERY 1236574686123083rtmssrv7 ... (8 Replies)
Discussion started by: aemunathan
8 Replies

9. Shell Programming and Scripting

Grouping and summing data through unix

Hi everyone, I need a help on Unix scripting. I have a file is like this Date Amt 20071205 10 20071204 10 20071203 200 20071204 300 20071203 400 20071205 140 20071203 100 20071205 100... (1 Reply)
Discussion started by: pcharanraj
1 Replies
Login or Register to Ask a Question