From: marc on
Hello,

Is it possible to replace a particular string at a particular
position ?

For example, 1 by 2 at position 3 :

0011223344
=>
0021223344

but not here (not 1 at position 3)
0001223344
=>
0001223344

I can replace any string at a position with sed : (.\{3\}\) but I
don't know how to test a string at the same time...

Thanks in advance.

From: Ben Bacarisse on
marc <marc.tessis(a)caramail.com> writes:

> Is it possible to replace a particular string at a particular
> position ?
>
> For example, 1 by 2 at position 3 :
>
> 0011223344
> =>
> 0021223344
>
> but not here (not 1 at position 3)
> 0001223344
> =>
> 0001223344
>
> I can replace any string at a position with sed : (.\{3\}\) but I
> don't know how to test a string at the same time...

Just include it in the pattern:

sed -e 's/^\(.\{2\}\)1/\12/'

--
Ben.
From: marc on
On 26 nov, 18:25, Ben Bacarisse <ben.use...(a)bsb.me.uk> wrote:

> Just include it in the pattern:
>
>   sed -e 's/^\(.\{2\}\)1/\12/'

Ah, yes, I missed it.

Many thanks !
From: Ed Morton on
marc wrote:
> Hello,
>
> Is it possible to replace a particular string at a particular
> position ?
>
> For example, 1 by 2 at position 3 :
>
> 0011223344
> =>
> 0021223344
>
> but not here (not 1 at position 3)
> 0001223344
> =>
> 0001223344
>
> I can replace any string at a position with sed : (.\{3\}\) but I
> don't know how to test a string at the same time...
>
> Thanks in advance.
>

Is this what you mean?

awk 'BEGIN{FS=OFS=""} {$3 = ($3==1 ? 2 : $3) }1'

Regards,

Ed.
From: Rakesh Sharma on
On Nov 26, 10:15 pm, marc <marc.tes...(a)caramail.com> wrote:
> Hello,
>
> Is it possible to replace a particular string at a particular
> position ?
>
> For example, 1 by 2 at position 3 :
>
> 0011223344
> =>
> 0021223344
>
> but not here (not 1 at position 3)
> 0001223344
> =>
> 0001223344
>
> I can replace any string at a position with sed :  (.\{3\}\) but I
> don't know how to test a string at the same time...
>
> Thanks in advance.


sed -e'
s/^.\{3\}/&\
/
s/1\n/2/;t
s/\n//
' yourfile