Unix addition ( Row wise)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix addition ( Row wise)
# 1  
Old 05-17-2007
Unix addition ( Row wise)

Hi

I have a file like

a,1
b,2
d,3
a,2
b,3
c,7


Result Desired:
a,3
b,5
d,3
c,7

i.e on the bases of 1st field the addition is done of the 2nd field and result printed out.

Thanks
# 2  
Old 05-17-2007
If output order is not a problem this simple perl script will do:

Code:
while(<>)
{
  chomp;
  @a= split(/,/);
  $sum{$a[0]} += $a[1];
}

foreach $key (keys %sum)
{
  print "$key,$sum{$key}\n";
}

# 3  
Old 05-17-2007
Code:
awk 'BEGIN{FS=","}
{ array[$1]+=$2 }
END{
for (i in array) print i,array[i] 
}' "file"

# 4  
Old 05-17-2007
If order is an issue
Code:
while(<>)
{
  chomp;
  @a= split(/,/);
  push (@order,$a[0]) if (!$sum{$a[0]});
  $sum{$a[0]} += $a[1];
}

foreach $key (@order)
{
  print "$key,$sum{$key}\n";
}

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Print row wise

Hi Help, I have an I/p, which looks like --- FF GG HH I want the o/p to be like --- FF GG HH. How we can do that? Thanks (7 Replies)
Discussion started by: Indra2011
7 Replies

2. Shell Programming and Scripting

Search/grep on row and column wise

Hello, I have a comma seperate metadata as follows: CITY ,COUNTY,STATE,COUNTRY NEW_YORK,NYC ,NY ,USA NEWARK ,ESSEX ,NJ ,USA CHICAGO ,COOK ,IL ,USA SEATTLE ,MINER ,WA ,USA In my process, I get two key values ie CITY NAME (can be one of the... (7 Replies)
Discussion started by: calredd
7 Replies

3. Shell Programming and Scripting

How to pass parameter to pipe command row-wise?

Hi, I have a list of parameter in a file. I want to pass them one by one to piped command and syntax is like <command> <parameter> <command continues> How to achieve that ? Thanks (1 Reply)
Discussion started by: ezee
1 Replies

4. Shell Programming and Scripting

calculating row-wise standard deviation using awk

Hi, I have a file containing 100,000 rows-by-120 columns and I need to compute for the standard deviation for each row. Any idea on how to calculate row-wise standard deviation using awk? My sample data looks like this: input data: 23 35 12 25 16 17 18 19 29 12 12 26 15 14 15 23 12 12... (2 Replies)
Discussion started by: ida1215
2 Replies

5. Shell Programming and Scripting

addition of values in row

file content are like this sam,22,29,23,24,25,26,22 i want to add the values from column 3 (fix column no) to as per user input say up to column 8 (variable as per user) can we do this without using "awk" for each column (as number of columns are variable as per user input ) Thanks in... (5 Replies)
Discussion started by: sagar_1986
5 Replies

6. Shell Programming and Scripting

Loop for row-wise averaging of multiple files using awk

Hello all, I need to compute a row-wise average of files with a single column based on the pattern of the filenames. I really appreciate any help on this. it would just be very difficult to do them manually as the rows are mounting to 100,000 lines. the filenames are as below with convention as... (2 Replies)
Discussion started by: ida1215
2 Replies

7. Shell Programming and Scripting

awk-gsub on column-wise on each row

awk '{ gsub(/....=/,""); print }' want.dat >final.dat the above awk command which removes all the chars before and including '=' on the entire row. --thats what it meant be.:) but i need to remove text on column-wise on each row. many thanks, EM ---------- Post updated at 10:00 AM... (4 Replies)
Discussion started by: elamurugu
4 Replies

8. Shell Programming and Scripting

User addition in Unix box

Hi All, Am using the following command to add a user in Unix box useradd -d <default_path> -g 90 -p <pwd for the user> <user_name> But am getting an error while using this command by root user.Let me know if this cmd is right or else is there any other command to add a user in unix... (9 Replies)
Discussion started by: Ashok_oct22
9 Replies

9. Shell Programming and Scripting

Addition of numbers in unix

Hi I have a file with specified format . Hxxxxxxxyyyyyggggggguuuuuuuuuurrrrrrrrrrrrrrrrrrrrrrrr xxxxxxxxyyyyyggggggguuuuuuuuuurrrrrrrrrrrrrrrrrrrrrrrr xxxxxxxxyyyyyggggggguuuuuuuuuurrrrrrrrrrrrrrrrrrrrrrrr xxxxxxxxyyyyyggggggguuuuuuuuuurrrrrrrrrrrrrrrrrrrrrrrr... (3 Replies)
Discussion started by: asinha63
3 Replies
Login or Register to Ask a Question