Add numbers in third code with conditions on the 1st and 2nd


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Add numbers in third code with conditions on the 1st and 2nd
# 1  
Old 01-13-2014
Add numbers in third code with conditions on the 1st and 2nd

Hello all,

I have a file with three columns, in which the first column has one letter from A-Z, the second one letter from A-Z, and the third a number:
e.g.
Code:
A A 5
A B 6
A C 1
A D 7
B A 8

The desired output is:
Code:
A A 5
A B 14
A C 1
A D 7

Just because A B and B A are essentially the same.

Is there a way to do this in awk or bash?

Thank you

Last edited by vbe; 01-13-2014 at 09:48 AM.. Reason: code tags...
# 2  
Old 01-13-2014
Yes, there is.

What have you tried so far?
# 3  
Old 01-13-2014
Hey admin,

Thanks for the interest.

I have written the following code that works, but it significantly slow.

calling ./counter.sh

Code:
#!/bin/bash
awk '{print $1}' $1 > 1.txt
awk '{print $2}' $1 > 2.txt
awk '{print $3}' $1 > 3.txt
paste 1.txt 2.txt | tr '\t' '_' > non-reverse.txt
paste 2.txt 1.txt | tr '\t' '_'> reverse.txt
paste non-reverse.txt 3.txt > tt; mv tt non-reverse.txt
paste reverse.txt 3.txt > tt; mv tt reverse.txt
cat reverse.txt non-reverse.txt > duplicates.txt
cat duplicates.txt | sort -nk1 | uniq > tt; mv tt duplicates.txt
awk '{print $1}' duplicates.txt | sort -nk1 | uniq > pair
for i in `cat pair`; do echo $i >> tt ; grep $i duplicates.txt | awk '{ sum+=$2} END {print sum}' >> tt; done
mv tt added_duplicates.txt
sed '$!N;s/\n/,/' added_duplicates.txt  | sort -nk1 | uniq > tt; mv tt final.txt
cat final.txt | tr '_' ' ' | tr ',' ' ' | grep ^[A-Z]  > tt; mv tt final.txt
rm -rf added_duplicates.txt non-reverse.txt reverse.txt duplicates.txt pair added_duplicates

# 4  
Old 01-13-2014
I haven't tried it but maybe you could give a try
Code:
awk '{i=($2<$1?$2" "$1:$1" "$2);A[i]+=$NF}END{for(k in A) print k,A[k]}' yourfile


Last edited by ctsgnb; 01-13-2014 at 12:53 PM.. Reason: Code fixed ... i used a lowercase instead of an upper case
# 5  
Old 01-13-2014
Hello ctsgnb,

It does not give any output...
# 6  
Old 01-13-2014
Code:
awk '
        {
                i = $2 FS $1
                if ( i in A )
                {
                        A[i] += $3
                        next
                }
                else
                        A[$1 FS $2] += $3
        }
        END {
                for ( k in A )
                        print k, A[k]
        }
' file

These 2 Users Gave Thanks to Yoda For This Post:
# 7  
Old 01-13-2014
hey Yoda,

Thanks for the reply.

I tested the code and works nicely!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Want the UNIX code - I want to sum of the 1st column wherever the first 2nd and 3rd columns r equal

I have the code for the below things.. File1 has the content as below 8859 0 subscriberCreate 18 0 subscriberPaymentMethodChange 1650 0 subscriberProfileUpdate 7668 0 subscriberStatusChange 13 4020100 subscriberProfileUpdate 1 4020129 subscriberStatusChange 2 4020307 subscriberCreate 8831... (5 Replies)
Discussion started by: Mahen
5 Replies
Login or Register to Ask a Question