help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] transpose


From: Val Krem
Subject: Re: [Help-bash] transpose
Date: Thu, 26 May 2016 22:58:35 +0000 (UTC)

Hi Pierre,

Thank you for your response.

here is the data that I wanted to transpose

9 A123 0
4 A123 1
8 A124 0
7 A124 1
9 A125 0
5 A125 1


to get
A123 9 4

A124 8 7
A125 9 5

I used you suggestion

awk '{if ($2==1) { one[$1]+=1;zero[$1]+=0} else {zero[$1]+=1;one[$1]+=0}}
END {for (k in  zero){ print k, zero[k], one[k]}}'


it did not give me the result. Here is what I got. 

4 1 0
5 1 0
7 1 0
8 1 0
9 2 0


What did I do wrong?

Val





On Thursday, May 26, 2016 1:38 AM, Pierre Gaston <address@hidden> wrote:
something like this should do it:

awk '{if ($2==1) { one[$1]+=1;zero[$1]+=0} else {zero[$1]+=1;one[$1]+=0}}
END {for (k in  zero){ print k, zero[k], one[k]}}'

the zero[$1]+=0 and one[$1]+=0 are just to make sure the array are
initialized for both values, there are probably more elegant or idiomatic
solutions but hopefully it's clear enough


On Thu, May 26, 2016 at 3:56 AM, Val Krem <address@hidden> wrote:

> Hi all,
>
> I am reading a file with  >5M records and with about 15 fields (variables).
>
> I am interested in two of the fields  (ID and Flag) and then two fields
> look like the following. The first field (ID) is alpha-numeric and the
> second field is numeric 0 or 1. My goal is to count the number of 0's and
> 1's by unique ID
>
>
> A123 0
> A123 0
> A123 0
> A123 1
> A123 1
> A123 1
> A123 0
> A123 0
> A123 0
> A123 0
> A123 1
> A123 0
> A123 0
> A124 0
> A124 0
> A124 1
> A124 1
> A124 1
> A124 1
> A124 0
> A124 1
> A124 0
> A124 0
> A124 1
> A124 0
> A124 1
> A124 0
> A124 0
> A125 0
> A125 0
> A125 1
> A125 1
> A125 1
> A125 1
> A125 0
> A125 0
> A125 0
> A125 0
> A125 0
> A125 0
> A125 1
> A125 0
>
> awk '{print $1, $2}' file1 |sort |uniq -c
> and got this
>
>
> 9 A123 0
> 4 A123 1
> 8 A124 0
> 7 A124 1
> 9 A125 0
> 5 A125 1
>
> Now I want the result to be  transposed like this or the
>
> desired output,
>
> A123 9 4
> A124 8 7
> A125 9 5
>
> I would appreciate if you help me out to get the desired result
>
>
> thank you in advance
>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]