Linux Platform

sed

Éditer des flux de texte avec des substitutions et transformations

texteremplacementstreamregex

Syntaxe

sed [OPTIONS] 'COMMANDE' [FICHIER...]

Options principales

OptionDescription
-iModifier le fichier en place
-nSupprimer la sortie automatique (utiliser avec p)
-eEnchaîner plusieurs commandes
s/ancien/nouveau/gSubstitution globale sur chaque ligne

Exemples

Remplacer un texte dans un fichier

$ sed -i 's/localhost/192.168.1.50/g' config.yaml
$ grep "host" config.yaml
  host: 192.168.1.50
  db_host: 192.168.1.50

Afficher uniquement certaines lignes

$ sed -n '5,10p' /var/log/auth.log
May 08 09:01:22 srv sshd[1234]: Accepted publickey for deploy
May 08 09:01:22 srv sshd[1234]: pam_unix(sshd:session): session opened
May 08 09:05:45 srv sudo: mouctar : TTY=pts/0 ; COMMAND=/bin/systemctl restart nginx

Supprimer les lignes vides

$ sed '/^$/d' script.sh | wc -l
47