Skip navigation

sed : éditeur de flux

Éditer les flux de sortie avec sed

Le commande sed permet d’éditer la sortie d’une commande. Cette commande permet également d’éditer directement un fichier comme le ferait Vim avec la même syntaxe.

: pour cette article le fichier .profile sera utilisé. Voici son contenu :

contenu du fichier

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
	. "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

Suprimer les espaces généré par la commande nl.

user@debian~$: nl bash_profile.txt | sed 's/^    //g'

sortie

 1	# ~/.profile: executed by the command interpreter for login shells.
 2	# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
 3	# exists.
 4	# see /usr/share/doc/bash/examples/startup-files for examples.
 5	# the files are located in the bash-doc package.

 6	# the default umask is set in /etc/profile; for setting the umask
 7	# for ssh logins, install and configure the libpam-umask package.
 8	#umask 022

 9	# if running bash
10	if [ -n "$BASH_VERSION" ]; then
11	    # include .bashrc if it exists
12	    if [ -f "$HOME/.bashrc" ]; then
13		. "$HOME/.bashrc"
14	    fi
15	fi

16	# set PATH so it includes user's private bin if it exists
17	if [ -d "$HOME/bin" ] ; then
18	    PATH="$HOME/bin:$PATH"
19	fi

Suprimer les espaces généré par la commande nl et mettre les numéros de ligne entre [crochet].

user@debian~$: nl bash_profile.txt | sed -e 's/^    //g' -e 's/^[ ]\?[0-9]\{1,2\}/\[&\]/g' -e 's/^\[ / \[/g'

sortie

 [1]	# ~/.profile: executed by the command interpreter for login shells.
 [2]	# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
 [3]	# exists.
 [4]	# see /usr/share/doc/bash/examples/startup-files for examples.
 [5]	# the files are located in the bash-doc package.

 [6]	# the default umask is set in /etc/profile; for setting the umask
 [7]	# for ssh logins, install and configure the libpam-umask package.
 [8]	#umask 022

 [9]	# if running bash
[10]	if [ -n "$BASH_VERSION" ]; then
[11]	    # include .bashrc if it exists
[12]	    if [ -f "$HOME/.bashrc" ]; then
[13]		. "$HOME/.bashrc"
[14]	    fi
[15]	fi

[16]	# set PATH so it includes user's private bin if it exists
[17]	if [ -d "$HOME/bin" ] ; then
[18]	    PATH="$HOME/bin:$PATH"
[19]	fi

: manipulation faites sous centOS 7 (Kernel: Linux 3.10.0-693.21.1.el7.x86_64)

by | March 22, 2018 | No Comments | Manipulation de fichier | Tags : sed