Rearranging columns

Hi,
I have an input file as follows :

input.txt

abcdTXXqwe
axdfSYYrew
dasgTXXqwt
gtfsTYYwer
gadfSXXerw
gwerSYYTXX

Now I have to get four output files.

output1.txt should have the first four cloumns, Where the rows containing 5th column as T and 6th-7th columns as XX
output2.txt should have the first four cloumns, Where the rows containing 5th column as T and 6th-7th columns as YY
output3.txt should have the first four cloumns, Where the rows containing 5th column as S and 6th-7th columns as XX
output4.txt should have the first four cloumns, Where the rows containing 5th column as S and 6th-7th columns as YY

i.e.

output1.txt

abcd
dasg

output2.txt

gtfs

output3.txt

gadf

output4.txt

axdf
gwer

I tried using cut and awk, but its too lengthy.

Is there any better solution?

Yes, with awk :D, but maybe shorter then yours:

awk 'BEGIN{
  a[TXX]="output1.txt"
  a[TYY]="output2.txt"
  a[SXX]="output3.txt"
  a[SYY]="output4.txt"}
{print substr($0,1,4) > a[substr($0,5,3)]}' file

Its erroring out saying

Check the format of your file at the positions 5 to 7, do you have more then 4 combinations?
Try this, it gives filenames as TXX SYY TYY etc, the 5 to the 7 positions of the lines:

awk '{print substr($0,1,4) > a[substr($0,5,3)]}' file
awk 'a=substr($1,5,3);b=substr($1,1,4);
a=="TXX"{print b > "output1.txt"}
a=="TYY"{print b> "output2.txt"}
a=="SXX"{print b> "output3.txt"}
a=="SYY"{print b> "output4.txt"}' input

-Devaraj Takhellambam

Thanks.