sed replace nth characters with string

Hi,

I hope you can help me out please?

I need to replace from character 8-16 with AAAAAAAA and the rest should stay the same after character 16

gtwrhtrd11111111rjytwyejtyjejetjyetgeaEHT
wrehrhw22222222hytekutkyukrylryilruilrGEQTH
hrwjyety33333333gtrhwrjrgkreglqeriugn;RUGNEURGU
gtwrhtrdAAAAAAAArjytwyejtyjejetjyetgeaEHT
wrehrhwAAAAAAAAhytekutkyukrylryilruilrGEQTH
hrwjyetyAAAAAAAAgtrhwrjrgkreglqeriugn;RUGNEURGU

Thank you very much

Any attempts from your side?

Yes but adds the the string infront

sed -r 's/^(........)./\1AAAAAAAA/' test

result

gtwrhtrdAAAAAAAA1111111rjytwyejtyjejetjyetgeaEHT

You need to give the search pattern something to replace. With BREs:

sed 's/\(.\{8\}\).\{8\}/\1AAAAAAAA/' file
gtwrhtrdAAAAAAAArjytwyejtyjejetjyetgeaEHT
wrehrhw2AAAAAAAAytekutkyukrylryilruilrGEQTH
hrwjyetyAAAAAAAAgtrhwrjrgkreglqeriugn;RUGNEURGU

If your sed handles EREs, try:

sed -E 's/(.{8}).{8}/\1AAAAAAAA/' file
gtwrhtrdAAAAAAAArjytwyejtyjejetjyetgeaEHT
wrehrhw2AAAAAAAAytekutkyukrylryilruilrGEQTH
hrwjyetyAAAAAAAAgtrhwrjrgkreglqeriugn;RUGNEURGU
2 Likes

superb thanks