J'ai un petit probl=C3=A8me avec 'sed'. Je souhaite effectuer le
remplacement d'une expression par une autre, mais uniquement une seule
fois dans un fichier.
Or, si je fais 'sed s/expr1/expr2/ monfichier', si expr1 est pr=C3=A9sent
plusieurs fois, elle sera remplac=C3=A9e =C3=A0 chaque fois.
Les recherches que j'ai effectu=C3=A9es ne parlent que du nombre de
substitutions (1, toutes, la derni=C3=A8re, etc...) sur une ligne, et pas
sur un fichier...
--
Lisez la FAQ de la liste avant de poser une question :
http://wiki.debian.net/?DebianFrench
Vous pouvez aussi ajouter le mot ``spam'' dans vos champs "From" et
"Reply-To:"
To UNSUBSCRIBE, email to debian-user-french-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
[â¦] 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/ s//yyy/ : remplacer la correspondance par yyy.
-- Sylvain Sauvage
Jacques L'helgoualc'h
David BERCOT a écrit, dimanche 24 février 2008, à 17:10 :
Bonjour,
bonsoir,
J'ai un petit problème avec 'sed'. Je souhaite effectuer le remplacement d'une expression par une autre, mais uniquement une seule fois dans un fichier. Or, si je fais 'sed s/expr1/expr2/ monfichier', si expr1 est présent plusieurs fois, elle sera remplacée à chaque fois. Les recherches que j'ai effectuées ne parlent que du nombre de substitutions (1, toutes, la dernière, etc...) sur une ligne, et pas sur un fichier...
Auriez-vous une piste ?
~ $ echo 'toto
]titi ]toto ]tata' > fic
~ $ sed -e '/toto/{s//Glop/;:A;N;bA}' <fic Glop titi toto tata
Variantes :
sed -e ':A;N;$!bA;s/toto/Glop/' <fic sed -ne 'H;${g;s/^n//;s/toto/Glop/;p}' <fic
Merci d'avance.
Hop zat elpse -- Jacques L'helgoualc'h
-- Lisez la FAQ de la liste avant de poser une question : http://wiki.debian.net/?DebianFrench Vous pouvez aussi ajouter le mot ``spam'' dans vos champs "From" et "Reply-To:"
To UNSUBSCRIBE, email to with a subject of "unsubscribe". Trouble? Contact
David BERCOT a écrit, dimanche 24 février 2008, à 17:10 :
Bonjour,
bonsoir,
J'ai un petit problème avec 'sed'. Je souhaite effectuer le
remplacement d'une expression par une autre, mais uniquement une seule
fois dans un fichier.
Or, si je fais 'sed s/expr1/expr2/ monfichier', si expr1 est présent
plusieurs fois, elle sera remplacée à chaque fois.
Les recherches que j'ai effectuées ne parlent que du nombre de
substitutions (1, toutes, la dernière, etc...) sur une ligne, et pas
sur un fichier...
Auriez-vous une piste ?
~ $ echo 'toto
]titi
]toto
]tata' > fic
~ $ sed -e '/toto/{s//Glop/;:A;N;bA}' <fic
Glop
titi
toto
tata
Variantes :
sed -e ':A;N;$!bA;s/toto/Glop/' <fic
sed -ne 'H;${g;s/^n//;s/toto/Glop/;p}' <fic
Merci d'avance.
Hop zat elpse
--
Jacques L'helgoualc'h
--
Lisez la FAQ de la liste avant de poser une question :
http://wiki.debian.net/?DebianFrench
Vous pouvez aussi ajouter le mot ``spam'' dans vos champs "From" et
"Reply-To:"
To UNSUBSCRIBE, email to debian-user-french-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
David BERCOT a écrit, dimanche 24 février 2008, à 17:10 :
Bonjour,
bonsoir,
J'ai un petit problème avec 'sed'. Je souhaite effectuer le remplacement d'une expression par une autre, mais uniquement une seule fois dans un fichier. Or, si je fais 'sed s/expr1/expr2/ monfichier', si expr1 est présent plusieurs fois, elle sera remplacée à chaque fois. Les recherches que j'ai effectuées ne parlent que du nombre de substitutions (1, toutes, la dernière, etc...) sur une ligne, et pas sur un fichier...
Auriez-vous une piste ?
~ $ echo 'toto
]titi ]toto ]tata' > fic
~ $ sed -e '/toto/{s//Glop/;:A;N;bA}' <fic Glop titi toto tata
Variantes :
sed -e ':A;N;$!bA;s/toto/Glop/' <fic sed -ne 'H;${g;s/^n//;s/toto/Glop/;p}' <fic
Merci d'avance.
Hop zat elpse -- Jacques L'helgoualc'h
-- Lisez la FAQ de la liste avant de poser une question : http://wiki.debian.net/?DebianFrench Vous pouvez aussi ajouter le mot ``spam'' dans vos champs "From" et "Reply-To:"
To UNSUBSCRIBE, email to with a subject of "unsubscribe". Trouble? Contact
-- Lisez la FAQ de la liste avant de poser une question : http://wiki.debian.net/?DebianFrench Vous pouvez aussi ajouter le mot ``spam'' dans vos champs "From" et "Reply-To:"
To UNSUBSCRIBE, email to with a subject of "unsubscribe". Trouble? Contact
--
Lisez la FAQ de la liste avant de poser une question :
http://wiki.debian.net/?DebianFrench
Vous pouvez aussi ajouter le mot ``spam'' dans vos champs "From" et
"Reply-To:"
To UNSUBSCRIBE, email to debian-user-french-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
-- Lisez la FAQ de la liste avant de poser une question : http://wiki.debian.net/?DebianFrench Vous pouvez aussi ajouter le mot ``spam'' dans vos champs "From" et "Reply-To:"
To UNSUBSCRIBE, email to with a subject of "unsubscribe". Trouble? Contact
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.
> s//yyy/ : remplacer la correspondance par yyy.
Par contre, je ne vois pas dans la doc que s//yyy/ remplace la "correspondance", mais je te crois sur parole (enfin, je ferais un test si nécessaire).
info sed, -> Addresses
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.
-- Jacques L'helgoualc'h
-- Lisez la FAQ de la liste avant de poser une question : http://wiki.debian.net/?DebianFrench Vous pouvez aussi ajouter le mot ``spam'' dans vos champs "From" et "Reply-To:"
To UNSUBSCRIBE, email to with a subject of "unsubscribe". Trouble? Contact
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.
> s//yyy/ : remplacer la correspondance par yyy.
Par contre, je ne vois pas dans la doc que s//yyy/ remplace la
"correspondance", mais je te crois sur parole (enfin, je ferais un
test si nécessaire).
info sed, -> Addresses
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.
--
Jacques L'helgoualc'h
--
Lisez la FAQ de la liste avant de poser une question :
http://wiki.debian.net/?DebianFrench
Vous pouvez aussi ajouter le mot ``spam'' dans vos champs "From" et
"Reply-To:"
To UNSUBSCRIBE, email to debian-user-french-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
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.
> s//yyy/ : remplacer la correspondance par yyy.
Par contre, je ne vois pas dans la doc que s//yyy/ remplace la "correspondance", mais je te crois sur parole (enfin, je ferais un test si nécessaire).
info sed, -> Addresses
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.
-- Jacques L'helgoualc'h
-- Lisez la FAQ de la liste avant de poser une question : http://wiki.debian.net/?DebianFrench Vous pouvez aussi ajouter le mot ``spam'' dans vos champs "From" et "Reply-To:"
To UNSUBSCRIBE, email to with a subject of "unsubscribe". Trouble? Contact
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.
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.
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.