26
Introduction au Syst` eme d’Exploitation Unix/Linux Utilitaire de fichier Unix : sed B. Jacob IC2 26 septembre 2017

Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/LinuxUtilitaire de fichier Unix : sed

B. Jacob

IC2

26 septembre 2017

Page 2: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Plan

1 Interet

2 Utilitaire sed

2/26

Page 3: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Interet

Plan

1 Interet

3/26

Page 4: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Interet

Qu’est ce que c’est ?

sed est un programme qui

applique des transformations sur des fichiers textes

possede leurs propres commandes d’edition→ est plus complexe a utiliser que les commandes deja vues

est un filtre

est disponible sur pratiquement tous les SE disposant decommandes en ligne.

4/26

Page 5: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Interet

Resume

Pour manipuler des fichiers textes on peut le faire par

l’utilitaires sed

→ outil puissant mais complexe a utiliser

les fonctions Search and Replace d’un editeur de texte→ ce sont des fonctions faciles a utiliser maisfastidieuses si nombre d’operations ↗

5/26

Page 6: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Interet

Dans quel cas l’utiliser ?

Analogie au decoupage de planches :

pour une planche : utilisation d’une scie a main→ mode d’emploi simple→ temps de traitement court→ on ne passe pas plus de temps a utiliser l’outil qued’apprendre a s’en servir( fonctions Search and Replace d’un editeur de texte)

pour un lot de planches : utilisation d’une scie circulaire demenuisier→ mode d’emploi plus complexe→ temps de traitement long mais automatique→ gain de temps par rapport au decoupage du lot de planchesavec une scie a main ( utilitaire sed)

6/26

Page 7: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Utilitaire sed

Plan

2 Utilitaire sed

7/26

Page 8: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Utilitaire sed

Presentation

sed = StreamEDitor

format :

sed [-n] [-e script ] [fichier ]

oused [-n] [-f fichier script ] [fichier ]

c’est un filtre, donc :Prend ses donnees (des lignes)

dans l’entree standard (clavier, par defaut) oudans un fichier (si [fichier ] positionne)

Affiche ses resultats sur

la sortie standard (ecran, par defaut) oune les affiche pas (si option [-n] positionnee)

8/26

Page 9: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Utilitaire sed

Presentation

sed [-n] [-e script ] [fichier ]

oused [-n] [-f fichier script ] [fichier ]

sed modifie les lignes a partir d’un script

script = commandes d’edition

en ligne si -e script (1 commande par -e)contenues dans un fichier si -f fichier script

9/26

Page 10: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Utilitaire sed

Commandes d’edition

[addresse [,adresse]] fonction [arguments]

selectionne les lignes selon les adresses

leur applique une fonction de sed avec ses arguments

10/26

Page 11: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Utilitaire sed

Adresses de sed

vide → toutes les lignes sont selectionnees

n → la ligne de numero n dans chaque fichier

$ → seulement la derniere ligne de chaque fichier

n1,n2 → node lignes entre n1 et n2

/expression reguliere/ → definit un contexte d’adresse

11/26

Page 12: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Utilitaire sed

Contexte d’Adresses

decrit le contexte dans lequel doivent etre les lignesselectionnees.

definit par une /expression reguliere/

sed supporte les expressions regulieres etendues(voir cours sur les Regexp)+ \n (NEWLINE)

12/26

Page 13: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Utilitaire sed

Exemples adresses sed

Transformation des lignes de tous les fichiers commencant par toto

sed -e "fonction [arguments]" toto.*

→ transformation sur toutes les lignes des fichiers

sed -e "12 fonction [arguments]" toto.*

→ transformation sur la 12e ligne de chaque fichier

sed -e "12,24 fonction [arguments]" toto.*

→ transformation sur sur les lignes 12 a 24 de chaque fichiersed -e "$ fonction [arguments]" toto.*

→ transformation sur la derniere ligne de chaque fichier

sed -e "/ [eE].*z$/ fonction [arguments]" toto.*

→ transformation sur les lignes commencant par e ou E et seterminant par z sur tous les fichiers

13/26

Page 14: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Utilitaire sed

Fonctions de sed

Il existe beaucoup de fonctions (Commandes d’edition)→ man sed

1 fonction = 1 caractere

Parmis les plus utilisees :

a (append) ajoute du texte

c (change) remplace la ligne

d (delete) efface la ligne

w fichier (write) ecrit la ligne dans fichier

14/26

Page 15: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Utilitaire sed

Exemple fonction ajout

Nom : a

Argument : ligne a inserer

Avec commande en ligne (option -e)

$ cat test_sed.txt

aaaaaa

bbbbbb

cccccc

$ sed -e "1 a \\zzzzzz" test sed.txt

aaaaaa

zzzzzz

bbbbbb

cccccc

15/26

Page 16: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Utilitaire sed

Exemple fonction ajout

Avec un fichier script (option -f)

$ cat test sed.txt

aaaaaa

bbbbbb

cccccc

$ cat script.sed

1 a \zzzzzz

$ sed -f script.sed test sed.txt

aaaaaa

zzzzzz

bbbbbb

cccccc

16/26

Page 17: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Utilitaire sed

Exemple fonction change

Nom : c

Argument : ligne qui remplace

$ cat test sed.txt

aaaaaa

bbbbbb

cccccc

$ cat script.sed

3 c \zzzzzz

$ sed -f script.sed test sed.txt

aaaaaa

bbbbbb

zzzzzz

17/26

Page 18: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Utilitaire sed

Exemple fonction delete

Nom : d

Argument : rien

$ cat test sed.txt

aaaaaa

bbbbbb

cccccc

$ cat script.sed

2 d

$ sed -f script.sed test sed.txt

aaaaaa

cccccc

18/26

Page 19: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Utilitaire sed

Exemple fonction write

Nom : w

Argument : nom du fichier ou l’on ecrit

$ cat test sed.txt

aaaaaa

bbbbbb

cccccc

$ cat script.sed

1 w toto.txt

$ sed -f script.sed test sed.txt

aaaaaa

bbbbbb

cccccc

$ cat toto.txt

aaaaaa

19/26

Page 20: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Utilitaire sed

Fonction substitute

C’est la plus utilisee

Nom : s

Argument : /reg-exp/remplacement/flags

reg-exp expression reguliere de sed

Stockage \( reg-exp \)Rappel \1 \2 \3 ...

flags :

g (global) (faire toutes les substitutions de la ligne)n avec n ∈ [1− 512] remplace seulement la nieme occurencep (print) si ok sort la ligne sur la sortie standardw fichier (write) si ok ecrit la ligne dans fichier

20/26

Page 21: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Utilitaire sed

Exemples simples

$ cat f

aaa bbb aaa bbb aaa

aaa ccc ddd

$ sed -e "s/aaa/AAA/" f → substitution 1re occurence

AAA bbb aaa bbb aaa

AAA ccc ddd

$ sed -e "s/aaa/AAA/g" f → substitution toutes lesoccurences

AAA bbb AAA bbb AAA

AAA ccc ddd

$ sed -e "s/aaa/AAA/3" f → substitution 3e occurence

aaa bbb aaa bbb AAA

aaa ccc ddd21/26

Page 22: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Utilitaire sed

Exemples simples

$ cat f

aaa bbb aaa bbb aaa

aaa ccc ddd

$ sed -e "s/ccc/CCC/w toto.txt" f

→ substitution + ecriture dans le fichier toto.txt

aaa bbb aaa bbb aaa

aaa ZZZ ddd

$ cat toto.txt

aaa ZZZ ddd

22/26

Page 23: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Utilitaire sed

Exemples simples

$ cat f

aaa bbb aaa bbb aaa

aaa ccc ddd

$ sed -e "s/aaa/AAA/p" f→ affichage contenu de f + affichage ligne substituee (bof)

aaa bbb aaa bbb aaa

aaa ZZZ ddd

aaa ZZZ ddd

⇒ Interet de l’option [-n]

$ sed -n -e "s/aaa/AAA/p" f→ seulement affichage ligne substituee

aaa ZZZ ddd

23/26

Page 24: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Utilitaire sed

Exemple avec stockage et rappel des champs

Fonction d’inversion des 2 premiers champs

$ cat f

aaaaa:bbbbbb:ccccc

ddddd:ee:ff

ggg:hhhhh:iiii

$sed -e ’s/\^\([^:]*\):\([^:]*\)/\2:\1/’ f

bbbbbb:aaaaa:ccccc

ee:ddddd:ff

hhhhh:ggg:iiii

24/26

Page 25: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Utilitaire sed

Exemples avec stockage et du rappel des champs

Synopsis

aaaaa:bbbbbb:ccccc

s/\^\([^:]*\):\([^:]*\)/\2:\1/

champ 1 champ 2

ligne entrée

ligne sortiebbbbbb:aaaaa\2:\1

25/26

Page 26: Introduction au Syst eme d’Exploitation Unix/Linuxperso.univ-lemans.fr/~bjacob/Enseignements/Shell_Unix/... · 2018. 11. 14. · Introduction au Syst eme d’Exploitation Unix/Linux

Introduction au Systeme d’Exploitation Unix/Linux

Utilitaire sed

Exercices

Exercices sur sed

26/26