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 !
Objet : Apprendre à utiliser les expressions rationnelles spécifiques à sed.
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.
Ou encore, elles servent à ajouter, à un endroit précisé par la regexp, un ou éléments au texte.
`-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
:
→ utilisation des expressions régulières de basiques, c'est-à-dire, celles qui utilisent les métacaractères simples (POSIX) qui sont en communs à tous les programmes utilisant les expressions régulières.
En voici un rappel :
*
, ^
, $
, [ ]
→ utilisation des classes :
[[:alnum:]] | Alpha-numerique [a-z A-Z 0-9] |
[[:alpha:]] | Alphabetic [a-z A-Z] |
[[:blank:]] | Espaces ou tabulations |
[[:cntrl:]] | Caractères de contrôle |
[[:digit:]] | Nombres [0-9] |
[[:graph:]] | Tous les caractères visibles (à l'exclusion des espaces) |
[[:lower:]] | Lettres minuscules [a-z] |
[[:print:]] | Caractères imprimables (tous caractères sauf ceux de contrôle) |
[[:punct:]] | Les caractères de ponctuation |
[[:space:]] | Les espaces |
[[:upper:]] | Les lettres majuscules [A-Z] |
[[:xdigit:]] | Chiffres hexadécimaux [0-9 a-f A-F] |
→ Il s'y ajoute le caractère &
: La chaîne à remplacer dans la chaîne de remplacement.
-r
:
→ On peut utiliser les caractères POSIX étendus (ceux communs à 'egrep'):
|
; ^
; $
; .
; *
; +
; ?
; ( )
; [
; { }
→ Il s'y ajoute enfin les raccourcis ci-desous. C'est là l'extension GNU qui n'est pas portable :
f
: Produit ou correspond à un saut
\n
: Produit ou correspond à un retour à la ligne
\r
: Produit ou correspond à un retour chariot
\t
: Produit ou correspond à un onglet horizontal
\v
: Produit ou correspond à une tabulation verticale
\w
: Correspond à n'importe quel caractère “mot”. Un caractère “mot” sera une lettre, un chiffre ou le caractère de soulignement.
\W
: Correspond à tout caractère «non-mot».
\b
: Correspond à une limite de mot.
\B
: Matches partout mais sur une limite de mot.
\a
: caractère alerte
\\dxxx
: Produit ou correspond à un caractère dont la valeur ascii décimal est xxx
\cx
: Control-x
\OXXX
: caractère par valeur aSCII octal
\xhh
: caractère par valeur aSCII hexadécimal.
Référence : sed, a stream editor, "Overview of Regular Expression Syntax"
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
echo "aaaa BBB cCcC # 12345" | sed 's+ +\n+g'
echo "aaaa BBB cCcC # 12345" | sed 's/ /\n/g'
echo "aaaa BBB cCcC # 12345" | sed 's_ _\n_g'
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
Exercices
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
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 # gagagagagagagagagaga BBB titi cCcC
sed 's/[[:alpha:]]*/gaga/g' sed1.txt
gaga gaga gaga gaga#gaga gaga1gaga2gaga3gaga4gaga5gaga gaga gaga gaga gaga/gaga/gaga
|
; ^
; $
; .
; *
; +
; ?
; ( )
; [
; { }
echo "coucou Hello Yep" | sed -r 's/[A-Z]{1}[a-z]{4}/OK/g'
coucou OK Yep
echo "ABC-123 abc-123" | sed -r 's/[A-Z]{3}-[[:digit:]]{3}\ [[a-z]{3}-[[:digit:]]{3}/OK/' OK
\
→ Les longues commandes peuvent être sectionnées avec :
\<retour à la ligne>[<espace>]
echo "123ab efab ef123" | sed -r 's/(ab[[:blank:]]ef){2}/OK/' 123OK123
echo "123ab efab ef123" | sed -r -e 's/(ab[[:blank:]]ef){2}/OK/g'\ -e 's/[[:digit:]]{3}/OK/g' OKOKOK
echo "abef cdef efgh abcd" | sed -r 's/ab|cd/OK/g' OKef OKef efgh OKOK
echo "lflfabef cdef efgh ftyabcdopm" | sed -r\ 's/^.*[ab|cd]*/OK/g' OK
echo "a aa aaa aaaa aaaaaa aaaaaaaa aaaaaaa" | sed -r\ 's/a{3,5}/OK/g' a aa OK OK OKa OKOK OKaa
echo "a aa aaa aaaa aaaaaa aaaaaaaa" | sed -r 's/a{3,}/OK/g' a aa OK OK OK OK
echo "a aa aaa aaaa aaaaaa aaaaaaaa" | sed -r 's/a{,3}/OK/g' OK OK OK OKOK OKOK OKOKOK
echo "a aa aaa aaaa aaaaaa aaaaaaaa" | sed -r\ 's/(a{1}[[:blank:]]|a{2}[[:blank:]])/OK/g' OKOKaOKaaOKaaaaOKaaaaaaaa
echo "a aa aaa aaaa aaaaaa aaaaaaaa" | sed -r\ -e 's/(a{1}[[:blank:]])/OK/' -e 's/(a{2}[[:blank:]])/OK/' OKOKaaa aaaa aaaaaa aaaaaaaa
echo "abc azbc azzbc azzzbc azzzzbc" | sed -r 's/az?bc/OK/g' OK OK azzbc azzzbc azzzzbc
echo "abc azbc azzbc azzzbc azzzzbc" | sed -r 's/az+bc/OK/g' abc OK OK OK OK
sed -r 's/a|c/gaga/g' sed1.txt
gagagagagagagaga BBB gagaCgagaC # 12345 BBB titi gagaCgagaC
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