sed find and replace command not working

Hi,
Am trying to replace a character '-' with 'O' in position 289 in my file but am not success with below command.

sed 's/^\(.\{289\}\)-/\1O/' filename
 
sed: 0602-404 Function s/^\(.\{289\}\)-/\1O/ cannot be parsed.

Thanks in Advance
Sara

I think sed has a limitation, you cannot specify more than 255 in curly braces "\{ \}", then you will get the following error:-

cannot be parsed.
1 Like

Is there any alternative for this issue?

try:

sed 's/^\(.\{200\}.\{89\}\)-/\1O/' filename
1 Like

It worked fine .
Great! Thanks alot.:smiley:

---------- Post updated at 04:34 PM ---------- Previous update was at 04:30 PM ----------

Is there way to write that into new file?

I tried with
sed 's/^\(.\{200\}.\{89\}\)-/\1O/' file1 > file2 but i got the content in the file2 with old data of file1.

Yes, you have to break the number 289=255+34:-

sed 's/^\(.\{255\}.\{34\}\)-/\1O/' infile

EDIT: make sure you have a - sign at this position, otherwise the replace will not happen.

1 Like

I got it Thanks!.:smiley:

for only the lines that got updated:

sed -n 's/^\(.\{200\}.\{89\}\)-/\1O/p' filename
2 Likes

Hi bipinajith,

i tried to use your code to replace the character at certain position. but it is not doing as desired, can u please assit

0979BB0979B I want to replace the 5th and 10th position.

Also, how can we do it in sed if we have to A-1, B-2, C-3, D-4 in different fields..

Currently i m using this code - any help will be appreciated..

 cat b.txt | sed -e 's/^\(.\{200\}.\{153\}\)B/\1X/' > xx.txt 

Replacing 5th & 10th position with C & D:-

echo 0979BB0979B  | sed 's/^\(.\{5\}\)B/\1C/; s/^\(.\{10\}\)B/\1D/'
0979BC0979D