Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
cancel
Showing results for 
Search instead for 
Did you mean: 

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
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

Peter Nikitka
Honored Contributor

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

Re: awk problem..any help..

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