Operating System - HP-UX
1752786 Members
5698 Online
108789 Solutions
New Discussion юеВ

Re: awk problem..any help..

 
SOLVED
Go to solution
amonamon
Regular Advisor

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 11
Ralph Grothe
Honored Contributor

Re: awk problem..any help..

man uniq
Madness, thy name is system administration
Steve Lewis
Honored Contributor

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
Steve Lewis
Honored Contributor
Solution

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])}'
Jonathan Fife
Honored Contributor

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
James R. Ferguson
Acclaimed Contributor

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...
Sandman!
Honored Contributor

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
Victor Fridyev
Honored Contributor

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
amonamon
Regular Advisor

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 :)
amonamon
Regular Advisor

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