Sed and SubString

Hi Folks,
I am here with a simple doubt. I am having a flat file in which I want to replace the characters from say 5 to 15 as some text.
Flat file contains a single line.

For example

01MRRAJESH21000RAJESH INDUSTRIES

In the above line pos 16-21 is Rajesh, I want to search for the substring based on the position and replace that position with some other text.

Please help. Thanks in advance!!!!

Use the {} to match the number of character occurence before the pattern ,

sed -r "s/^.{15}RAJESH/REPLACE/g"

Hi Karthi,
Thanks for your reply
I do not have any pattern in my flat file. It is just a simple text. I want to replace the text say from position 20 to 30 with my text.
Also,my shell is unable to recognize sed-r. I am using korn shell.

Hi

Assuming ABCEFG is the string to be replaced with:

echo 01MRRAJESH21000RAJESH INDUSTRIES | sed 's/\(.\{15\}\)RAJESH\(.*\)/\1ABCEFG\2/'

Guru.

1 Like

Try:

awk '{split(val,V,"");for(i=0;i<=p2-p1;i++)$(i+p1)=V[i+1]}1' val=newstring p1=10 p2=18 FS= OFS= infile

input:

01MRRAJESH21000RAJESH INDUSTRIES

output:

01MRRAJESnewstringESH INDUSTRIES
1 Like