awk problem..any help.. (154 Views)
Reply
Regular Advisor
amonamon
Posts: 166
Registered: ‎05-21-2006
Message 1 of 12 (154 Views)
Accepted Solution

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

Honored Contributor
Ralph Grothe
Posts: 2,631
Registered: ‎04-09-2000
Message 2 of 12 (154 Views)

Re: awk problem..any help..

man uniq
Madness, thy name is system administration
Honored Contributor
Steve Lewis
Posts: 725
Registered: ‎05-18-2000
Message 3 of 12 (154 Views)

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
Steve Lewis
Posts: 725
Registered: ‎05-18-2000
Message 4 of 12 (154 Views)

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
Jonathan Fife
Posts: 256
Registered: ‎07-19-2006
Message 5 of 12 (154 Views)

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
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 6 of 12 (154 Views)

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
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 7 of 12 (154 Views)

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
Victor Fridyev
Posts: 937
Registered: ‎07-01-1998
Message 8 of 12 (154 Views)

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
Regular Advisor
amonamon
Posts: 166
Registered: ‎05-21-2006
Message 9 of 12 (154 Views)

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 :)
Regular Advisor
amonamon
Posts: 166
Registered: ‎05-21-2006
Message 10 of 12 (154 Views)

Re: awk problem..any help..

and can U please explain
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
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 11 of 12 (154 Views)

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"
Regular Advisor
amonamon
Posts: 166
Registered: ‎05-21-2006
Message 12 of 12 (154 Views)

Re: awk problem..any help..

Thanks Peter...I read about awk..but those arrays are confusing me little...thank U for your help..

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.