Converting Multiline Files to Flat Files?

How to convert this:
F1-R1 F1-R2 F1-R3 into a flat file for bash?? Each record

F2-R1 F2-R2 F2-R3
F3-R1 F3-R2 F3-R3
F4-R1 F4-R2 F4-R3

is on one line with all fields for that record, put into an output file. The output file should look like this when converted:

F1-R1,F2-R1,F3-R1,F4-R1
F1-R2,F2-R2,F3-R2,F4-R2
F1-R3,F2-R3,F3-R3,F4-R3

An awk solution:-

awk '
        {
                for ( i = 1; i <= NF; i++ )
                        A[NR FS i] = $i
        }
        END {
                for ( i = 1; i <= NF; i++ )
                {
                        for ( j = 1; j <= NR; j++ )
                        {
                                if ( j == NR )
                                        printf "%s\n", A[j FS i]
                                else
                                        printf "%s,", A[j FS i]
                        }
                }
        }
' OFS=, file

Hello bud1738,

Could you please try following and let me know if this helps.

xargs -n1 < Input_file | pr -4 -t

Output will be as follows.

F1-R1             F2-R1             F3-R1             F4-R1
F1-R2             F2-R2             F3-R2             F4-R2
F1-R3             F2-R3             F3-R3             F4-R3

Thanks,
R. Singh

try also:

xargs -n1 < infile | sort -t"-" -k2 | paste -d, - - - -

Hello rdrtx1,

I think above code doesn't meet OP's output, because OP wants to transpose the columns to rows. Other had above code will only change number of columns to 4.

Thanks,
R. Singh

Yes that works! What would I do though if say something kept reoccurring, for example

a1 a2 a3
b1 b2 b3
c1 c2 c3
d1 d2 d3
a1 a2 a3
b1 b2 b3
c1 c2 c3
d1 d2 d3
etc...

and convert it into

a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
a1 b1 c1 d1
a2 b2 c2 d2
etc....

Hello bud1738,

Please use code tags as per forum rules for commands/codes/Inputs which you are using into your posts, could you please try following and let me know
if this could help(I haven't tested though).

awk '{!A[$1];!B[$2];!C[$3];}END{for(i in A){Q=Q?Q OFS i:i};for(j in B){U=U?U OFS j:j};for(k in C){V=V?V OFS k:k};print Q ORS U ORS V}'  Input_file
 

Output will be as follows.

a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
 

Thanks,
R. Singh