awk help

Hi all,

Input file: file.txt
_____________________

7789 0
3445 1
7789 1
1223 0
4567 0
7789 0
7789 0
1212 1
3445 0
7789 1
3445 1


How can I get the output as:

<number> <no_0_entries> <no_1_entries>

i.e.

7789 3 2
3445 1 2
1223 1 0
4567 1 0
1212 0 1

I have no luck in solving this, please help . Thank you.

What have you tried?

I tried upto this

$ awk '$2==0' file.txt > file.txt.0
$ awk '$2==1' file.txt > file.txt.1

$ cat file.txt.0
7789 0
1223 0
4567 0
7789 0
7789 0
3445 0

$ cat file.txt.1
3445 1
7789 1
1212 1
7789 1
3445 1

<0 entries>
$ awk '{count[$1]++}END{for(j in count) print j,count[j]}' file.txt.0
1223 1
4567 1
7789 3
3445 1

<1 entries>
$ awk '{count[$1]++}END{for(j in count) print j,count[j]}' file.txt.1
7789 2
3445 2
1212 1

Could anyone help me on this please. Thank you.

How to achieve the req output, please guide.

u can try creating array with $1 and $2 as the key.

A possible solution with awk :

awk '
{
   numbers[$1]++;
   zero[$1] += ($2 == 0 ? 1 : 0);
    one[$1] += ($2 == 1 ? 1 : 0);
}
END {
   for (i in numbers) {
      print i,zero,one
   }
}' inputfile

Jean-Pierre.

Jean-Pierre, thank you so much, it worked. Thank you again.