M. Beausoleil
M. Beausoleil

Reputation: 3555

Transpose number not separated in columns using Bash

I've tried the tr function to transpose in bash but it's not working. e.g. tr 'abcd' 'jkmn'...

The idea is to take a series of numbers:

92121
92911

and switch them so that they would look like this:

99
22
19
21
11

Here is a test dataset:

echo "92121
      92911
      29222
      22222
      22222
      22222" > ~/Desktop/output.geno

I know we can separate numbers using the cut function.

I feel that I could use

for var in 1:96
do
   tmp=$(cut -c var output.geno)
   tr $tmp
done

Upvotes: 0

Views: 71

Answers (2)

Akshay Hegde
Akshay Hegde

Reputation: 16997

Input

$ cat file
92121
92911
29222
22222
22222
22222

Output

$ awk '
{   
    n=split($0,F,r)
    for (i=1; i in F; i++)
        a[NR,i] = F[i]
}
n>p { p = n }
END {    
    for(j=1; j<=p; j++) 
    {
        str=a[1,j]
        for(i=2; i<=NR; i++)
        {
            str=str a[i,j];
        }
        print str
    }
}' file
992222
229222
192222
212222
112222

Upvotes: 0

RavinderSingh13
RavinderSingh13

Reputation: 133528

@M. Beausoleil: Try: Let's say Input_file is as follows.

cat  Input_file
92121
92911
29222
22222
22222
22222

Following code could help in same.

awk '{for(i=1;i<=length($0);i++){array[i]=array[i]?array[i] substr($0,i,1):substr($0,i,1)};MAX=MAX>i?MAX:i} END{for(j=1;j<MAX;j++){print array[j]}}'   Input_file

NON-one liner form of above solution too as follows.

awk '{
        for(i=1;i<=length($0);i++){
                                        array[i]=array[i]?array[i] substr($0,i,1):substr($0,i,1)
                                  };
        MAX=MAX>i?MAX:i
     }
     END{
                for(j=1;j<MAX;j++){
                                        print array[j]
                                  }
     }
    '   Input_file

Output will be as follows.

992222
229222
192222
212222
112222

Upvotes: 1

Related Questions