Vous n'êtes pas identifié(e).
L'icône rouge permet de télécharger chaque page du wiki visitée au format
PDF et la grise au format ODT →
Ceci est une ancienne révision du document !
Avec sed les ER s'utilisent lorsqu'on veut modifier un fichier texte.
Il s'agit de matcher un ou des éléments d'un fichier texte par une REGEXP et de remplacer, ce ou ces éléments ciblés du texte, par autres choses ou rien.
`-r' `--regexp-extended' Use extended regular expressions rather than basic regular expressions. Extended regexps are those that `egrep' accepts; they can be clearer because they usually have less backslashes, but are a GNU extension and hence scripts that use them are not portable. *Note Extended regular expressions: Extended regexps.
-r
:
Sed couvre des expressions régulières de base (POSIX). Il s'agit des caractères de bases qui sont communs à tous les programmes utilisant les expressions régulières.
En voici un rappel :
*
, ^
, $
, [ ]
Les classes :
[:alpha:]
, [:blank:]
, [:cntrl:]
, [:digit:]
, [:graph:]
, [:lower:]
, [:print:]
, [:punct:]
, [:space:]
, [:upper:]
, [:xdigit:]
Il s'y ajoute &
: La chaîne à remplacer dans la chaîne de remplacement.
-r
:
Il s'y ajoute les caractères POSIX étendus :
|
; ^
; $
; .
; *
; +
; ?
; ( )
; [
; { }
Il s'y ajoute enfin quelques un caractères que l'on retrouve sous Perl :
\b
, \B
, \w
, \W
, \s
, \S
, \`
, \
', \<
, \>
Ainsi que :
\a
(caractère alerte), \f
(saut), \n
(saut de ligne), \r
(retour chariot), \
t (tabulation horizontale), \v
(tabulation verticale), \cx
(Control-x ), \dxxx
(caractère en valeur ascii décimal), \OXXX
(caractère par valeur aSCII octal), \xhh
(caractère par valeur aSCII hexadécimal).
Pour utiliser les REGEXP, il faut avant tout maîtriser les syntaxes de substitution, et l'adressage.
s/modèle/remplacement/[drapeau] fichier
ou
cmd | s/modèle/remplacement/[drapeau]
s/motif/substitut/ -> si motif = une regexp -> alors subtitut = une chaîne de caractère
Pour REGEXP, on peut utiliser la syntaxe habituelle, ou la syntaxe étendue avec l'option-r
.
echo "aaaa BBB cCcC # 12345" > ~/sed1.txt
sed -i 's+ +\n+g' ~/sed1.txt && cat sed1.txt
aaaa BBB cCcC # 12345
d
sed '3,4 d' ~/sed1.txt
aaaa BBB 12345
pas d'option : rien n'est modifié
d
: commande de suppression
3,4
: ligne 3 et 4
-e
et répétition le commande d
:sed -e '3d; 4d' ~/sed1.txt
aaaa BBB 12345
sed '/^B/d' ~/sed1.txt
aaaa cCcC # 12345
echo "/coucou/titi" >> sed1.txt && cat sed1.txt aaaa BBB cCcC # 12345 BBB titi cCcC /coucou/titi
sed 's/\/coucou\/titi/gaga/' sed1.txt
aaaa BBB cCcC # 12345 BBB titi cCcC gaga
Qu'on peut remplacer par :
sed 's_/coucou/titi_gaga_' sed1.txt
aaaa BBB cCcC # 12345 BBB titi cCcC gaga
sed '/^B/,/^1/d' ~/sed1.txt
aaaa
n
et m
sont des nombres entiers naturels
On remplace chaine 1 par chaine 2 de la ligne n à la ligne m.
On remplace chaine 2 par chaine 3 pour la ou les ligne(s) adressées par chaine 1.
cat sed1.txt aaaa BBB cCcC # 12345 BBB titi cCcC
sed '2,6s/BBB/GAGA/' sed1.txt
ou
sed -r '2,6s/B{3}/GAGA/' sed1.txt
aaaa GAGA cCcC # 12345 GAGA titi cCcC
sed '/BBB/s/BBB/GAGA/' sed1.txt
ou
sed -r '/B{3}/s/BBB/GAGA/' sed1.txt
aaaa GAGA cCcC # 12345 GAGA titi cCcC
cat sed1.txt aaaa BBB cCcC # 12345 BBB titi cCcC
sed 's/./GAGA/' sed1.txt GAGAaaa GAGABB GAGACcC GAGA GAGA2345 GAGABB GAGAiti cCcC
sed 's/. /GAGA/' sed1.txt aaaa BBB cCcC # 12345 BBB titGAGAcCcC
Ou
sed '/./s/#/GAGA/' sed1.txt aaaa BBB cCcC GAGA 12345 BBB titi cCcC
sed '/^#/ d' ~/sed1.txt
aaaa BBB cCcC 12345 BBB titi cCcC
sed '/^#/ !d' ~/sed1.txt
#
sed '/[C]$/s/[Cc]./GAGA/g' sed1.txt aaaa BBB GAGAGAGA # 12345 BBB titi GAGAGAGA
sed '/[C]$/s/[Cc]./GAGA/g' sed1.txt
aaaa BBB GAGAGAGA # 12345 BBB titi GAGAGAGA
sed '/[C]$/s/[Cc].*/GAGA/g' sed1.txt
aaaa BBB GAGA # 12345 BBB titi GAGA
sed '/[1-5]/ d' ~/sed1.txt
aaaa BBB cCcC # BBB titi cCcC
sed 's/[1-5]/gaga/g' sed1.txt
aaaa BBB cCcC # gagagagagagagagagaga BBB titi cCcC
sed '/[1-5]/s/[1-5]*/gaga/g' sed1.txt
aaaa BBB cCcC # gaga BBB titi cCcC
sed 's/[0-9][0-9]*$/[&]/' sed1.txt
aaaa BBB cCcC # [12345] BBB titi cCcC
sed 's/[0-9][0-9]*$/(&)/' sed1.txt
aaaa BBB cCcC # (12345) BBB titi cCcC
sed 's/[:digit:]/gaga/g' sed1.txt
aaaa BBB cCcC # 12345 BBB gagagagagagagaga cCcC
MAIS
sed 's/[:space:]/gaga/g' sed1.txt gagagagagagagaga BBB gagaCgagaC # 12345 BBB titi gagaCgagaC
sed 's/a|c/gaga/g' sed1.txt
aaaa BBB cCcC # 12345 BBB titi cCcC
sed -r 's/a|c/gaga/g' sed1.txt
gagagagagagagaga BBB gagaCgagaC # 12345 BBB titi gagaCgagaC
sed 's/(a|B).*/gaga/g' sed1.txt
aaaa BBB cCcC # 12345 BBB titi cCcC <code user> hypathie@debian:~$ sed -r 's/(a|B).*/gaga/g' sed1.txt
gaga gaga cCcC # 12345 gaga titi cCcC hypathie@debian:~$
sed 's/a{1,4}/gaga/g' sed1.txt
aaaa BBB cCcC # 12345 BBB titi cCcC
sed -r 's/(a|B).*/gaga/g' sed1.txt
gaga gaga cCcC # 12345 gaga titi cCcC