Showing results for
Do you mean

SOLVED

# awk problem..any help..

100107 1 1000
100129 1 1000
100136 1 1000
100136 1 1000
100144 1 1000
100144 1 1000
100148 1 1000
100181 1 1000

is there a way to get file like this:

100107 1 1000
100129 1 1000
100136 2 2000
100144 2 2000
100148 1 1000
100181 1 1000

any hints?

Thanks

11 REPLIES
Honored Contributor [Founder]

## Re: awk problem..any help..

man uniq
Honored Contributor [Founder]

## Re: awk problem..any help..

associative arrays.

awk '{c[\$1]++; t[\$1]+=\$3}END{for(f in c)printf("%s %d %d\n",f,c[f],t[f])}' input_filename

Did the trick for me, see the evidence:

awk '{c[\$1]++; t[\$1]+=\$3}END{for(f in c)printf("%s %d %d\n",f,c[f],t[f])}'
100107 1 1000
100129 1 1000
100136 1 1000
100136 1 1000
100144 1 1000
100144 1 1000
100148 1 1000
100181 1 1000

100107 1 1000
100129 1 1000
100144 2 2000
100148 1 1000
100181 1 1000
100136 2 2000
Honored Contributor [Founder]

## Re: awk problem..any help..

I assumed that you wanted the 2nd column to be a count, not a sum of the values. If you want to sum \$2 as well, make this simple change:

awk '{c[\$1]+=\$2; t[\$1]+=\$3}END{for(f in c)printf("%s %d %d\n",f,c[f],t[f])}'
Honored Contributor [Founder]

## Re: awk problem..any help..

Assuming the duplicate rows will always be adjacent like they were in your example:

awk 'BEGIN {prev1="BEGIN";prev2=0;prev3=0}
{if (\$1 == prev1) {prev2=prev2 + \$2;
prev3=prev3 + \$3;
continue}
if (prev1 != "BEGIN") {print prev1 " " prev2 " " prev3}
prev1=\$1;prev2=\$2;prev3=\$3}
END {print prev1 " " prev2 " " prev3}' filename

I thought the associated arrays approach was nicer, but there can never be too many examples :)
Decay is inherent in all compounded things. Strive on with diligence
Acclaimed Contributor [Founder]

## Re: awk problem..any help..

Hi:

I prefer Perl.

# perl -ne '\$line{\$_}++;END{for \$key (sort keys %line) {@a=split/ /,\$key;print join " ",\$a[0],\$line{\$key},\$a[2]}}' file

...yields (in sorted output order:

100107 1 1000
100129 1 1000
100136 2 1000
100144 2 1000
100148 1 1000
100181 1 1000

Regards!

...JRF...
Honored Contributor [Founder]

## Re: awk problem..any help..

Hi amonamon...try the awk construct below:

# awk '{f[\$1]++;s[\$1]+=\$3} END{for(i in f) print i,f[i],s[i]}' infile > outfile

~cheers
Honored Contributor [Founder]

## Re: awk problem..any help..

Hi,

sort -n inputfile| uniq -c |awk '{print \$1,\$2,\$4}'

HTH
Entities are not to be multiplied beyond necessity - RTFM

## Re: awk problem..any help..

to Steve Lewis I want 2 col. to be sum not count of values...:)) but thanks for your very useful help :)

## Re: awk problem..any help..

awk '{c[\$1]+=\$2; t[\$1]+=\$3}END{for(f in c)printf("%s %d %d\n",f,c[f],t[f])}' file >file1

I am new in awk and working with arrays in awk...so this confuses me little...

THANKS a LOOTT

Honored Contributor [Founder]

## Re: awk problem..any help..

Hi,

read 'man awk' as first information - you'll find a section for ARRAYs.
For short:

c[\$1]+=\$2
In array c add the value of second field (\$2) to the array element indexed by the value of the first field (\$1). Note that awk uses associative arrays, i.e. an index needs not to be a number.

for(f in c)
Looop through all indices of the array c using f as current idex value. Thre will be no sorting of indices when running through the loop.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"