how to introduce a space in a single column without distrubing the other columns

Hello Experts,

I am new to this forum, I would like to do the following changes in one of the column of a txt file, which is having around 9 column.

For example, column 3 is having letters like this

AB11
AB12
C
CA
CB
AC1
AC2

I would like to convert the same column as follows

1AB1
2AB1
 C
 CA
 CB 
 AC1
 AC2

if it is having less than four letters, then the program need to introduce a space first and the the letter. If it is having four letters, then the program should bring the last number to the first place.

Need to do the conversion only in that particular column and the rest should be as such.

Can any one help me in this regard?

Expecting your replies and thanks in advance.

Regards
Fredrick.

With sed:

$> cat infile
AB11
AB12
C
CA
CB
AC1
AC2
$> sed 's/^.\{1,3\}$/ &/g; s/^\([^ ]..\)\(.\)$/\2\1/g' infile
1AB1
2AB1
 C
 CA
 CB
 AC1
 AC2

awk alternative:

$ awk -v FS="" 'NF==4{$0=$4 $1 $2 $3}NF<4{$0=" " $0}1' file

Thank you very much for your reply. The input file i mentioned here, is having 9 columns and i need to change the above mentioned, only in 3rd column.

Is there any other way to do this?

expecting your replies and thanks in advance.

Regards
Fredrick.

It's easier when provided with a realistic sample file from the beginning.

$ cat f
col1 col2 AB11 col4
col1 col2 AB12 col4
col1 col2 C col4
col1 col2 CA col4
col1 col2 CB col4
col1 col2 AC1 col4
col1 col2 AC2 col4

$ awk 'length($3)==4{$3=substr($3, 4) substr($3,1,3)}length($3)<4{$3=" " $3}1' f
col1 col2 1AB1 col4
col1 col2 2AB1 col4
col1 col2  C col4
col1 col2  CA col4
col1 col2  CB col4
col1 col2  AC1 col4
col1 col2  AC2 col4

Hi Zaxxon,

$> sed 's/^.\{1,3\}$/ &/g; s/^\([^ ]..\)\(.\)$/\2\1/g' infile

Thats cool, its working fine. But the input file i mentioned here, is having 9 columns and i need to change the above mentioned, only in the 3rd column.

Is there any other way to do this?

Expecting your replies and thanks in advance.

Regards
Fredrick.

---------- Post updated at 05:06 PM ---------- Previous update was at 04:55 PM ----------

Hi Ripat,

Good, its working fine..
Thank you..

regards
Fredrick.