Re: Substitution par 'sed' une seule fois dans un fichier...
Le 13933ième jour après Epoch,
Jacques L'helgoualc'h écrivait:
> François TOURDE a écrit, dimanche 24 février 2008, à 23:32 :
>> Le 13933ième jour après Epoch,
>> Sylvain Sauvage écrivait:
>>
>> > Sylvain Sauvage, dimanche 24 février 2008, 18:37:49 CET
>> >>[???]
>> >> M???enfin, bon, il y a sûrement d???autres solutions???
>> >
>> > Ben oui, il y a plus simple : 0,/xxx/s//yyy/
>> >
>> > 0,/xxx/ : la commande ne s???applique que de la 1re ligne à celle qui
>> > correspond à /xxx/
>>
>> Je crois qu'il vaut mieux traduire ça par: "entre la ligne 1 et la
>> première contenant xxx", non?
>
> Non, c'est bien de la ligne 1 à la ligne de la /regexp/, incluses (c'est
> peut-être la même) ; le truc de la ligne 0 est une extension GNU.
En relisant, je me suis rendu compte que ce que j'avais lu la première
fois de son explication n'avait rien à voir. J'avais lu: "La commande
ne s'applique qu'à la première ligne contenant xxx"... Bref, j'étais
dans le gaz encore :) Ou alors c'est le Médoc...
> The empty regular expression `//' repeats the last regular
> expression match (the same holds if the empty regular expression is
> passed to the `s' command). Note that modifiers to regular
> expressions are evaluated when the regular expression is compiled,
> thus it is invalid to specify them together with the empty regular
> expression.
> [...]
> GNU `sed' also supports some special two-address forms; all these
> are GNU extensions:
> `0,/REGEXP/'
> A line number of `0' can be used in an address specification like
> `0,/REGEXP/' so that `sed' will try to match REGEXP in the first
> input line too. In other words, `0,/REGEXP/' is similar to
> `1,/REGEXP/', except that if ADDR2 matches the very first line of
> input the `0,/REGEXP/' form will consider it to end the range,
> whereas the `1,/REGEXP/' form will match the beginning of its
> range and hence make the range span up to the _second_ occurrence
> of the regular expression.
>
> Note that this is the only place where the `0' address makes
> sense; there is no 0-th line and commands which are given the `0'
> address in any other way will give an error.
Merci pour ces explications détaillées. Mais ne doit-on pas considérer
que 1,/xxx/ qui matche la seconde occurence n'est pas plutôt un bug?
Dans ce cas, que devrait faire /xxx/,/xxx/ ?
D'autre part, je viens de faire le test sur ma machine, que addr1 soit
0 ou 1, le match marche sur la première ligne.
Ok, je sais, je coupe les cheveux en sqrt(2), mais bon...
Reply to: