Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

occurences

Status
Not open for further replies.

Murugs

Technical User
Jun 24, 2002
549
US
I have a text file for column a and b which looks like this

a b
18772 2
18324 4
18009 2
18772 3
18772 3
18772 2
18009 2
18772 3
18009 3
17654 2
18772 3

I need the output
a b c
18772 2 2
18772 3 4
18009 2 2
18009 3 1
18324 4 1
17654 2 1

where c means
no of occurences for value a against value b.

Regards
MP
 
BEGIN {
FS=OFS=" ";
}

{
arr[$1,$2]++;
}

END {
for (i in arr) {
split(i, ind, SUBSEP);
printf("%s %s %s %s %s\n", ind[1], OFS, ind[2], OFS, arr);
}
}
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Hello vlad
BIG thanks for ur script.
It did the job..
But if possible shall you explain the script line by line.

Thanks
MP
 
BEGIN {
FS=OFS=&quot; &quot;;
}

{
#
# make the FIRST and the SECOND columns indecies to the
# associative array &quot;arr&quot; - the 'arr' is a two-dimentional
# array. Increment [++] the value of the array's cell
# [indexed by &quot;$1,$2&quot;].
#
arr[$1,$2]++;
}

END {
#
# iterate through array 'arr' - current index is 'i'
for (i in arr) {
# split the current index 'i' into an array 'ind'.
# the FIRST cell of 'ind' is used to be column ONE
# the SECOND cell of 'ind' is used to be column TWO

split(i, ind, SUBSEP);
# The value of arr is an accumulated count of
# occurances.
# Print out the statistics.
printf(&quot;%s %s %s %s %s\n&quot;, ind[1], OFS, ind[2], OFS, arr);
}
} vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top