A general solution for transposing with awk follows.
To work correctly we need the number of columns.
That could be found while reading the file into an array of values:
#!/bin/bash file=i4 delimiter="" sep="" transpose() { : # comment sed for newer awks. # Do this to separate characters in quite old awk # very old wak does not allow that the FS could be Null. #sed -e 's/./ &/g' "$file" | awk ' { for(i=1;i<=NF;i++){a[NR,i]=$i};{(NF>m)?m=NF:0} } END { for(j=1; j<=m; j++) { for(i=1; i<=NR; i++) { b=((a[i,j]=="")?" ":a[i,j]) printf("%s%s",(i==1)?"":sep,b) } printf("\n") } } ' FS="$delimiter" sep="$sep" cc="$countcols" <"$file" } transpose
With this file:
abc fghij klmn opqrs
Will print:
afko bglp chmq inr j s
Awk takes care of separating the characters if the "field separator" is null.
The characters are printed in one line if the variable sep is also null.
If the awk available is an older one, a null FS is not valid. Use the following two commands.
To count the number of characters, use this in older awks:
# Work with any POSIX awk to find the max character count in all rows. countcols=$(awk '{l=length($0);(l>max)?max=l:0}END{print max}' < "$file")
To do the transposition, an space could be added in front of each character and use an space as a "field separator" and avoid the empty FS:
sed -e 's/./ &/g' < "$file" | awk ' {for(i=1;i<=cc;i++){if($i==""){$i=" "};r[i]=r[i]sep$i;};sep=""}; END{for(i=1;i<=cc;i++)print(r[i])} ' cc="$countcols"
Comment the sed line for newer awks.