calculating unique strings values

Hi,

Im looking for a script which will calculate the unique strings column 2 & 3 values in a log as mentioned in example

eg:-

bag  12   12 
bag  18   15 
bags  15   13 
bags  15   14 
blazer  24   24 
blazer  33   32 
boots  19   15 

Result should be:-

bag 30 27
bags 30 27
blazer 57 56
boots 19 15

Please help on this

Regards
wintech

Try:

awk '{a[$1]+=$2;b[$1]+=$3}END{for (i in a) print i,a,b}' file
1 Like
$
$ cat f60
bag 12 12
bag 18 15
bags 15 13
bags 15 14
blazer 24 24
blazer 33 32
boots 19 15
$
$ perl -lane '$i=0; map{$x{$F[0]}->[$i++]+=$_} @F[1,2]}{for (sort keys %x){print "$_ @{$x{$_}}"}' f60
bag 30 27
bags 30 27
blazer 57 56
boots 19 15
$
$

tyler_durden

1 Like

If infile is sorted:

awk 'p!=$1{if(p)print p,s,t;s=t=x;p=$1}{s+=$2;t+=$3}END{print p,s,t}' infile

otherwise:

sort infile | awk 'p!=$1{if(p)print p,s,t;s=t=x;p=$1}{s+=$2;t+=$3}END{print p,s,t}'
1 Like

Thank You!!! Hurray it works perfect..... Thank you so much

---------- Post updated at 11:47 PM ---------- Previous update was at 11:34 PM ----------

Hi,

i have another small request, need to remove double/triple space in between a content to single space in a file as below example.

File
bags 15 13
bags 15 14
blazer 24 24
blazer 33 32
boots 19 15

Result should be single space between the content
bags 15 13
bags 15 14
blazer 24 24
blazer 33 32
boots 19 15

Thanks
wintech

sed 's/  */ /g' file_many_spaced >file_single_spaced
tr -s ' ' <file_many_spaced >file_single_spaced

Hi,

Sorry!! double space is not removed to single space.

Try this

Are you sure it is a matter of space char ? or is it about <tab> char ?
Please upload your file and copy paste exactly the commande you tried.

If you're using awk, why not do it all in awk in the first place?

awk '{ A[$1]+=$2; B[$1]+=$3; X[$1]++ } END { for(Y in X) print Y, A[Y], B[Y] }'

It will accept arguments as single or multiple spaces or tabs, and output as single spaces...