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!

using awk to do data griouping

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
HI,
I need help about using SOLARIS AWK(it may be different from GNU awk) in doing data grouping read from a file
the file is like

1.01 5555 333
1.52 33kk jkj
1.70 jnbn bkj
2.5
3.0

I want to count records with first record column
1 < ($1) < 2
then count for 2 < ($1) < 3 and so on then print the result in a file using the format :
1.0 3
2.0 1 and so on..
 
Hi hanona,

This should do what you want.

{
j = 0
f1 = $1
while (f1 >= 1) {
j++
f1--
}
cnt[j]++
if (j>max) max = j
}
END {
for (j=1;j<=max;j++) {
printf(&quot;%3.1f %d\n&quot;,j,cnt[j])
}
}

Hope this helps.

CaKiwi
 
This is a simpler solution. I inadvertently posted this starting a new thread. Here it is replying to the right thread.

{
j = int($1)
cnt[j]++
if (j>max) max = j
}
END {
for (j=1;j<=max;j++) {
printf(&quot;%3.1f %d\n&quot;,j,cnt[j])
}
}

CaKiwi


 

Hi hanona and CaKiwi!

My little contribution - awk one-liner
(but with some limitations: first field shows
integer, and records will be (maybe)
unsorted):

Code:
awk '{ val[int($1)] ++ }  END { for (i in val) print i, val[i] }' inputfile

If statement &quot;{ val[int($1)] ++ }&quot; makes
problems, you can replace it with
{ ind = int($1); val[ind] ++ }.

Bye!

KP.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top