Identifying same character and ouput the sum


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Identifying same character and ouput the sum
# 1  
Old 02-22-2007
Identifying same character and ouput the sum

Hi all,

I have a list of data below.
As long as there are same character in the 1st column (eg xxx ), i would want to add all the numbers in the 3rd column and display as the output below so the total would be 10+20+30 = 60. Can anybody help to do this using nawk? Using solaris by the way.
Expected output is display below.

Input:
xxx bbb 10
xxx ccc 20
xxx ddd 30
yyy bbb 100
yyy ccc 200
yyy ddd 300


Output:
xxx bbb 60
yyy bbb 600
# 2  
Old 02-22-2007
Try...
Code:
nawk '{a[$1]+=$3}END{for(x in a)print x, a[x]}' Input > Output

# 3  
Old 02-26-2007
Quote:
Originally Posted by Ygor
Try...
Code:
nawk '{a[$1]+=$3}END{for(x in a)print x, a[x]}' Input > Output

Hi Ygor,

Can i assign a variable to the output of a[x]? Lets say NUM = a[x] and print NUM instead ?
# 4  
Old 02-26-2007
Code:
nawk '{a[$1]+=$3}END{for(x in a){ NUM=a[x]; print NUM}}' Input > Output

# 5  
Old 02-26-2007
In addition,

Can your code perform the below?

Input:
xxx bbb 10
xxx ccc 20
xxx ddd 30
yyy bbb 100
yyy ccc 200
yyy ddd 300


Output:
xxx bbb 10
xxx ccc 20
xxx ddd 30 60
yyy bbb 100
yyy ccc 200
yyy ddd 300 600
# 6  
Old 02-26-2007
try this
Code:
awk '{a[$1]=( length(a[$1]) == 0 ? $0 : a[$1]"\n"$0 );sum[$1]+=$3} END{for(x in a)print a[x] " " sum[x] } ' file

# 7  
Old 03-21-2007
Quote:
Originally Posted by anbu23
Code:
nawk '{a[$1]+=$3}END{for(x in a){ NUM=a[x]; print NUM}}' Input > Output


Hi,

If my input is below, can this code above be modified such that only the code can identify the unique term in column 1. In the below, since xxx and yyy are the only 2 different terms in column 1, it will only output xxx and yyy

Input
xxx bbb 10
xxx ccc 20
xxx ddd 30
yyy bbb 100
yyy ccc 200
yyy ddd 300


Ouput
xxx
yyy
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to ouput not in file2 using awk?

Hi All, Seeking for your assistance on how to ouput the file which is not match in file 1 and file 2 using awk. I tried NR=FNR but it's not working, it will only show the match record. Ex. File1 abc def ghi File2 23a gd abc Output: abc (2 Replies)
Discussion started by: znesotomayor
2 Replies

2. Shell Programming and Scripting

<< Filter and Format the ouput >>

Hi All, I have a output like below $ cat aa.lst Value of output parameters --------------------------------------- Parameter Name : SNAPSHOTTIMESTAMP Parameter Value : 2014-01-07-15.21.50.022423 Parameter Name : DATABASESIZE Parameter Value : 96178176 ... (2 Replies)
Discussion started by: kamauv234
2 Replies

3. Shell Programming and Scripting

Parse ouput within an AWK Command

I have a script that I am writing to parse the output of information from a command. Here is one of the lines of the output exported into the variable OUTPUT: export OUTPUT=”name_of_system,0,5,9,55,ip_address,another_value,/PATH/OF/A/VALUE/I/NEED" I can get the output I need... (5 Replies)
Discussion started by: jake0391S
5 Replies

4. Shell Programming and Scripting

Format Ouput

I have this input file Switch 0; Sun Sep 11 12:40:53 2011 EDT (GMT+4:00) 12:40:53.159984 SCN Port Offline;g=0x1e4b6 A2,P0 A2,P0 379 NA 12:40:53.159991 *Removing all nodes from port A2,P0 A2,P0 379 NA 18:45:31.326604 Port Elp engaged ... (1 Reply)
Discussion started by: greycells
1 Replies

5. Shell Programming and Scripting

View ouput as a file

Hi all , I have a view in teradata , the ouput of that view have to be stored as a file with delimitere as '|'.Is there any possibility of doing this in unix ? Thanks in advance , Vinoth (6 Replies)
Discussion started by: vino.paal
6 Replies

6. Shell Programming and Scripting

Print sum and relative value of the sum

Hi i data looks like this: student 1 Subject1 45 55 Subject2 44 55 Subject3 33 44 // student 2 Subject1 45 55 Subject2 44 55 Subject3 33 44 i would like to sum $2, $3 (marks) and divide each entry in $2 and $3 with their respective sums and print for each student as $4 and... (2 Replies)
Discussion started by: saint2006
2 Replies

7. UNIX for Dummies Questions & Answers

Finding string and getting formatted ouput

I have follwoing text file as alarm dump. A1/EXT "B25I11K0150_F W" 512 090629 1121 RADIO X-CEIVER ADMINISTRATION BTS EXTERNAL FAULT MO RSITE CLASS RXOCF-405 DKRD01_INDS 1 EXTERNAL ALARM MAINS FAIL ... (1 Reply)
Discussion started by: lalchand
1 Replies

8. Shell Programming and Scripting

Cannot redirect ouput?????

Hello experts, I'm testing a program that prints error message to the screen. I want to redirect the output to a file using >. but the message only prints on screen and not writing to the file, Any suggestion on what I might try? (3 Replies)
Discussion started by: minifish
3 Replies

9. Windows & DOS: Issues & Discussions

Wrong Ouput when using printf under SFU

I used printf to print the following under SFU, lrs=`cat lrs` hrs=`cat hrs` tp=`cat tp` printf “\n\n%5sM = $lrs Ohms%5sX = $hrs Ohms%5sT = $tp %%\n\n\n” > file cat file With the above script, I used %% after $tp only as a percentage sign and I get only the following output: % T =... (5 Replies)
Discussion started by: ilak1008
5 Replies

10. Shell Programming and Scripting

capture the ouput!

Hi, my perl script is calling another external java program. The Java in turn prints out a string. how can I capture the string. ------------------ #!/usr/bin/perl print "Content-type:text/html\n\n"; use CGI; $query = new CGI; $theCookie = $query->cookie('someCookie'); $user =... (0 Replies)
Discussion started by: azmathshaikh
0 Replies
Login or Register to Ask a Question