Addition as part of uniq command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Addition as part of uniq command
# 1  
Old 09-20-2011
Data Addition as part of uniq command

Dear Friends,

I want to know if it is possible to perform addition as part of uniq command.

Input:
213.64.56.208 1
213.64.56.208 2
213.64.56.208 3
213.46.27.204 10
213.46.27.204 20
213.46.27.204 30

The above input should be converted as follows
213.64.56.208 6
213.46.27.204 60

The comparison should be done only with the first field (ip address). the summation of second field should be presented as the second field in the result. If this cannot be done with "uniq" command. Please let me know other ways to do the same.

Thanks in advance,
# 2  
Old 09-20-2011
Code:
awk '{a[$1]+=$2;}END{for (i in a) print i, a[i];}' file

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 09-20-2011
Thanks Guruprasad,

I would like to know if this is possible with any commands other than awk family. Please clarify.
# 4  
Old 09-21-2011
Quote:
Originally Posted by tamil.pamaran
I would like to know if this is possible with any commands other than awk family. Please clarify.

It can be done with any programming language, e.g., perl, python, ruby, shell (while loop), sed, etc....
# 5  
Old 09-21-2011
Why not awk? It can be done in nearly any language, of course, but:

1) awk is especially good and efficient for this particular problem
2) everyone has an awk of some sort

Doing the same problem in generic sh, for instance:
Code:
while read A B
do
        if [ "$LAST" != "$A" ]
        then
                [ -z "$LAST" ] || echo "$LAST" "$TOTAL"
                LAST="$A"
                TOTAL="$B"
        else
                TOTAL=`expr $TOTAL + $B`
        fi
done < datafile

[ -z "$LAST" ] || echo "$LAST" "$TOTAL"

...and it only works if the data's all in order. If it's not, you need to do
Code:
sort < datafile > /tmp/$$
while read A B
do
        if [ "$LAST" != "$A" ]
        then
                [ -z "$LAST" ] || echo "$LAST" "$TOTAL"
                LAST="$A"
                TOTAL="$B"
        else
                TOTAL=`expr $TOTAL + $B`
        fi
done < /tmp/$$
rm -f /tmp/$$

13 lines, n+1 extra processes, a sort operation, and a temp file for something awk handles natively and faster, in one compact line.

Would it help if the awk code were better explained?

Code:
awk '
# This block gets run once per line.  $1 is the first column and $2 is the second.
# A += B acts like A = A + B.
# awk arrays act like perl hashes, so A["string"] is valid.
# So the line might do:
#         a["213.64.56.208"] = a["213.64.56.208"] + 3
# ...and so keep a running total for each different value of column 1.
{a[$1]+=$2;}
# This block gets run only once, after EOF.
# "for (i in a)" loops through every array index in a.
# "print i, a[i];" prints the index, then a field separator(space by default),
# then the total held in the array.
END{for (i in a) print i, a[i];}' file


Last edited by Corona688; 09-21-2011 at 06:41 PM..
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trying to find the distinct lines using uniq command

Platform :Oracle Linux 6.4 Shell : bash The below file has 7 lines , some of them are duplicates. There are only 3 distinct lines. But why is the uniq command still showing 7 ? I just want the distinct lines to be returned. $ cat test.txt SELECT FC.COORD_SET_ID FROM OM_ORDER_FLOW F, -... (2 Replies)
Discussion started by: kraljic
2 Replies

2. Shell Programming and Scripting

Command line arguments for addition

Hi all, I am trying to write a code for addition of n numbers which will be passed by the user as command line arguments. I wrote the following code. add=0 for (( i = 1 ; i <= $# ; i++ )) do add=`expr $i + $add` done #echo "sum is : $add" input : $./add.sh 12 32 14... (7 Replies)
Discussion started by: PranavEcstasy
7 Replies

3. Shell Programming and Scripting

cat and uniq command

Hello i have tried to retrieve a distinct list of data from a file with a command combination but the uniq command line is not filtering the fields repeated. Can someone help me ? here is the file input 3837734|LAUNCH TEST01 3837735|LAUNCH TEST01 3837736|LAUNCH TEST01 3837737|LAUNCH... (4 Replies)
Discussion started by: ade05fr
4 Replies

4. UNIX for Dummies Questions & Answers

Re: How To Use UNIQ UNIX Command On single Column

Hi , Can You Please let Know How use unix uniq command on a single column for deleting records from file with Below Structure.Pipe Delimter File . Source Name | Account_Id A | 101 B... (2 Replies)
Discussion started by: anudeepkumar123
2 Replies

5. UNIX for Advanced & Expert Users

uniq command

at which situation uniq command fails. How to delete/remove duplicate lines in an unix file (1 Reply)
Discussion started by: tp2115
1 Replies

6. Shell Programming and Scripting

Uniq Command?

Hello, I have a file with a list of car makes and specific information for each make. An example is: @Audi:Warranty @Audi:Pricing @Audi:Colors @Acura:Warranty @Acura:Pricing @Acura:Colors and so on through a bunch of makes. I need to make a list in a selection box of just one name of... (12 Replies)
Discussion started by: macbb1117
12 Replies

7. UNIX for Dummies Questions & Answers

Several file comparison not uniq or comm command

When comparing several files is there a way to find values unique to each file? File1 a b c d File2 a b t File 3 a (3 Replies)
Discussion started by: dr_sabz
3 Replies

8. UNIX for Dummies Questions & Answers

Uniq command behaving odd

My file has k s j v l k a s f l k s a d f j l a s (3 Replies)
Discussion started by: phoenix_nebula
3 Replies

9. Shell Programming and Scripting

Help with uniq command

I call.... cat 1.txt | uniq -c Sample of whats in 1.txt vmstat cd exit w cd cd cd newgrp xinit f cd cd cd rlogin (2 Replies)
Discussion started by: Bandit390
2 Replies

10. UNIX for Dummies Questions & Answers

uniq command???

HI, Please tell the usage of uniq command in UNIX with example Thanks (2 Replies)
Discussion started by: skyineyes
2 Replies
Login or Register to Ask a Question