Operating System - Linux
1755574 Members
4039 Online
108836 Solutions
New Discussion юеВ

Re: replacing according to position

 
SOLVED
Go to solution
Reen
Advisor

replacing according to position

Hi all,
can anybody help in this
In a line, i have a pattern tat is to be replaced.

using sed , i can replace in a particular line, but tat pattern can occur in the line in many other places.
i want only one instance to be replaced.
14 REPLIES 14
Reen
Advisor

Re: replacing according to position

Attached is the file.
I want the 2nd instance of Blore to b replaced in 2nd line.
But in the actual file am not sure of the occurance of this instance.It can be 3rd instance.
But the position is fixed.
so in this case, 23-27 characters in 2nd line to be replaced by Mlore
Dennis Handly
Acclaimed Contributor

Re: replacing according to position

>using sed, i can replace in a particular line, but that pattern can occur in the line in many other places. i want only one instance to be replaced.

One instance per line, or one per whole file?
For the former, don't add "g" to the "s" function, then it only will replace one.

If it is once per file, you'll need to write an awk script. To replace to the first then copy the rest of the file.
Reen
Advisor

Re: replacing according to position

Thanks Dennis.
Can u give me a solution for my attached file
Yogeeraj_1
Honored Contributor

Re: replacing according to position

hi Reen@,

In the vi editor, can you try this:
:1,$s/.Blore/Mlore

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Dennis Handly
Acclaimed Contributor

Re: replacing according to position

>Yogeeraj: 1,$s/.Blore/Mlore

You may need to quote the "." otherwise it would match the first:
:1,$s/\.Blore/Mlore

(But I guess it was safe since there are no chars before the first Blore.)

>But the position is fixed. so in this case, 23-27 characters in 2nd line to be replaced by Mlore

That makes it easy:
sed -e 's/^\(......................\)Blore/\1Mlore/'

(There should be 22 dots. The \1 puts them back into the output. If only one the second line, you can use '2s/'.)
James R. Ferguson
Acclaimed Contributor
Solution

Re: replacing according to position

Hi:

A bit more readable (and much easier to maintain) is the specificatino of the number of character repetitions, thusly:

# sed -e 's/^\(.\{22\}\)Blore/\1Mlore/'

Note that the curly braces must be escaped with a backslash just like the parentheses must be.

Regards!

...JRF...
Reen
Advisor

Re: replacing according to position

Hi all,
Thanks a lot for the help.
One more query is that,
is ther any restriction on the no. of characters?
I mean.. in a line can i change the 260-264th character in the similar manner?
I hav to achieve that.
Dennis Handly
Acclaimed Contributor

Re: replacing according to position

>is there any restriction on the no. of characters?

No, you can use JRF's improvement, and you just type in the number.
^\(.\{259\}\)
Reen
Advisor

Re: replacing according to position

Dennis,
Didnt get JRF`s improvement???