0

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
Community
  • 1
  • 1
M. Beausoleil
  • 3,141
  • 6
  • 29
  • 61

2 Answers2

1

@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
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
0

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
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36