Change nth Char in a file..

I wanted to Replace if 20th Char is space then Replace by X ..

12345678901234567890

AAAA HEXW PROGRM01 (Ended by 3 Spaces ) Followed by junk(can be spaces als)

BBBB HEXW PROGRM01 A0121225001 (Ended by 3 Spaces)Followed by junk

I have Tired some thing of this sort ...

cat temp |sed 's/\(.\{19\}\)\([ ]\)\(.*$\)/\1X\3/'

was searching for any 19 Chars follwed by space followed by any chars till end of file...

but the out put is

EN33 HEXW ENIPOS01 X
EN33 HEXW ENIPOS01 A0121225001X

where as i wanted

only

EN33 HEXW ENIPOS01 X
EN33 HEXW ENIPOS01 A0121225001

If I try for 20 th Char as "A" it works...
But if i put Space instead of "A" It behaves Differently

cat temp |sed 's/\(.\{19\}\)\(A\)\(.*$\)/\1X\3/' --- Works..

out put
AAAA HEXW PROGRM01
BBBB HEXW PROGRM01 X0121225001

cat temp |sed 's/\(.\{19\}\)\([ ]\)\(.*$\)/\1X\3/' --- Works..

out put
AAAA HEXW PROGRM01 X
BBBB HEXW PROGRM01 A0121225001X

It is working fine for me.

$ cat file
EN33 HEXW ENIPOS01  
EN33 HEXW ENIPOS01 A0121225001
$ sed 's/\(.\{19\}\)\([ ]\)\(.*$\)/\1X\3/' file
EN33 HEXW ENIPOS01 X
EN33 HEXW ENIPOS01 A0121225001
$ sed 's/\(.\{19\}\) /\1X/' file
EN33 HEXW ENIPOS01 X
EN33 HEXW ENIPOS01 A0121225001
$ sed 's/.\{19\} /&X/' file
EN33 HEXW ENIPOS01  Xanbu
EN33 HEXW ENIPOS01 A0121225001
sed 's/^\(.\{19\}\) /\1X/' temp

Thanks aigles

It Works...

I should have searched for starting with... :slight_smile:

Anbu...

If We have Space Appended to both the Lines it doesnt work...
I have tried with ^\(.\{19\}\ and its fine now...

Thanks again..