sum(col) finding from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sum(col) finding from a file
# 1  
Old 04-22-2008
sum(col) finding from a file

Hi

I have an file which looks like

country address phone amount
sweden |address |phone | 10 |
Singapo |address |phone | 20 |
Italy-N |address |phone | 30 |
denmar |address |phone | 40 |

Here i need to do the sum(amount), how to do this in shell scripting

Thanks
Babu
# 2  
Old 04-22-2008
Code:
awk -F'|' 'NF>1 {sum+=$4} END {print sum}' inputfile

Jean-Pierre.
# 3  
Old 04-22-2008
Quote:
Originally Posted by aigles
Code:
awk -F'|' 'NF>1 {sum+=$4} END {print sum}' inputfile

Jean-Pierre.
first line is not going to disturb the sum by anyway ..

the check NF>1 is not needed though

Code:
awk -F'|' ' {sum+=$4} END {print sum}' filename

# 4  
Old 04-22-2008
Hi

I have an file it looks like

USSR|8650|262|Asia
Canada|3852|24|North America
China|3692|866|Asia
USA|3615|219|North America
Brazil|3286|116|South America
India|1269|637|Asia
Argentina|1072|26|South America
Sudan|968|19|Africa
Algeria|920|18|Africa

I have written a shell program which is not working. Pl help me where i am going wrong

FILE=/path/a.sh
area=0
total=0

for area in `grep a.sh | awk -F'|' '{print $2}'`
do
total=$(($total+$area))
done
echo $total

I tried with matrixmadhan and aigles, But it is not working.

Thanks
Babu
# 5  
Old 04-22-2008
Your grep command is incomplete. Do you realy need this grep ?
Code:
FILE=/path/a.sh
area=0
total=0

for area in `awk -F'|' '{print $2}' $FILE`
do 
total=$(($total+$area))
done
echo $total

or
Code:
FILE=/path/a.sh
total=0

total=$(awk -F'|' ' {sum+=$4} END {print sum}' $FILE)
echo $total

Jean-Pierre.
# 6  
Old 04-22-2008
Quote:
Originally Posted by aigles
Your grep command is incomplete. Do you realy need this grep ?
Code:
FILE=/path/a.sh
area=0
total=0

for area in `awk -F'|' '{print $2}' $FILE`
do 
total=$(($total+$area))
done
echo $total

or
Code:
FILE=/path/a.sh
total=0

total=$(awk -F'|' ' {sum+=$4} END {print sum}' $FILE)
echo $total

Jean-Pierre.
Thanks Jean It is working, I used grep becoz whether the file is found or not

Babu
# 7  
Old 04-23-2008
USSR|8650|262|Asia
Canada|3852|24|North America
China|3692|866|Asia
USA|3615|219|North America
Brazil|3286|116|South America
India|1269|637|Asia
Argentina|1072|26|South America
Sudan|968|19|Africa
Algeria|920|18|Africa
***************

FILE=/path/a.sh
area=0
total=0

for area in `awk -F'|' '{print $2}' $FILE`
do
total=$(($total+$area))
done
echo $total

to my above shell scripts is working fine for one sum(column). How to use more than one col for sum ie $2 and $3 i want the sum

Thanks
Babu
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help in finding sum for values in 2 different fields

Hi there, I have 2 files in following format cat file_1 Storage Group Name: aaaa HBA UID SP Name SPPort ------- ------- ------ 0 21 Storage Group Name: bbbb HBA UID... (2 Replies)
Discussion started by: jpkumar10
2 Replies

2. Shell Programming and Scripting

Modifying col values based on another col

Hi, Please help with this. I have several excel files (with and .xlsx format) with 10-15 columns each. They all have the same type of data but the columns are not ordered in the same way. Here is a 3 column example. What I want to do add the alphabet from column 2 to column 3, provided... (9 Replies)
Discussion started by: newbie83
9 Replies

3. Shell Programming and Scripting

Run a program-print parameters to output file-replace op file contents with max 4th col

Hi Friends, This is the only solution to my task. So, any help is highly appreciated. I have a file cat input1.bed chr1 100 200 abc chr1 120 300 def chr1 145 226 ghi chr2 567 600 unix Now, I have another file by name input2.bed (This file is a binary file not readable by the... (7 Replies)
Discussion started by: jacobs.smith
7 Replies

4. Shell Programming and Scripting

Printing from col x to end of line, except last col

Hello, I have some tab delimited data and I need to move the last col. I could hard code it, awk '{ print $1,$NF,$2,$3,$4,etc }' infile > outfile but it would be nice to know the syntax to print a range cols. I know in cut you can do, cut -f 1,4-8,11- to print fields 1,... (8 Replies)
Discussion started by: LMHmedchem
8 Replies

5. UNIX for Advanced & Expert Users

Print line based on highest value of col (B) and repetion of values in col (A)

Hello everyone, I am writing a script to process data from the ATP world tour. I have a file which contains: t=540 y=2011 r=1 p=N409 t=540 y=2011 r=2 p=N409 t=540 y=2011 r=3 p=N409 t=540 y=2011 r=4 p=N409 t=520 y=2011 r=1 p=N409 t=520 y=2011 r=2 p=N409 t=520 y=2011 r=3 p=N409 The... (4 Replies)
Discussion started by: imahmoud
4 Replies

6. Shell Programming and Scripting

how to add new col in a file

Hi, Experts, I have a requirement as following: my source file: a a a b b c c c c I need add one more colume as following: 1 a 2 a 3 a 1 b 2 b 1 c 2 c (4 Replies)
Discussion started by: ken002
4 Replies

7. Shell Programming and Scripting

Finding Sum of Occurrances

Hello friends, I have a database export and with this export i need to find some statistics which is required. I take the export of a table with only 3 columns like this: 2nd 3rd 12th 123 05.11.2010 09:27 0 124 05.11.2010 09:28 0 125 05.11.2010 09:34 1 126 05.11.2010 09:39 0... (5 Replies)
Discussion started by: EAGL€
5 Replies

8. Shell Programming and Scripting

Finding the sum of two numbers

cat *.out |grep "<some text>" | awk '{print $6}' For ex,This will reutrn me 11111 22222 is it possible to add these two numbers in the above given command itself?I can write this to a file and find the sum. But I prefer to this calculation in the above given line itself. Any... (3 Replies)
Discussion started by: prasperl
3 Replies

9. Shell Programming and Scripting

Compare - 1st col of file

Hi, I have two different files, one has two columns and other has only one column. I would like to compare the first column in the first file with the data in the second file and write a third file with the data that is not present is not common to them. First file:... (26 Replies)
Discussion started by: swame_sp
26 Replies

10. Ubuntu

Match col 1 of File 1 with col 1 File 2 and create a 3rd file

Hello, I have a 1.6 GB file that I would like to modify by matching some ids in col1 with the ids in col 1 of file2.txt and save the results into a 3rd file. For example: File 1 has 1411 rows, I ignore how many columns it has (thousands) File 2 has 311 rows, 1 column Would like to... (7 Replies)
Discussion started by: sogi
7 Replies
Login or Register to Ask a Question