Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Sed ( Stream EDitor)

Sed is a powerful text processing tool that can be used to perform basic
text transformations on an input stream.

sed [option] 'fuction' <file>

Some functions: d → delete

s → substitute

y → transform.

We can separate between functions using ";"

$sed ' ' file ←→ cat file

$sed -n ' ' file → does not print any lines by default. Silence mode.

$sed -i file → edits file in place. (GNU)

Sed can have a script as an input.

Case 1: writing the script in the command line using the option $sed -e

Case 2: using the option $sed -f script_file.

A bash script is a file containing a sequence of commands that are executed


by the bash program line by line. It allows you to perform a series of
actions, such as navigating to a specific directory, creating a folder, and
launching a process using the command line.

$sed '4d ; 7d' test.txt → delete the 4th line and the 7th line.

$sed '4,7d' test.txt → delete lines 4 through 7.

Sed dressing patterns (motif /fr) are used to perform operations on text
files in Linux.

/regex/

Sed '/^# / d' test.txt → delete all lines starting with '#'

Interval with '/motif1/ , /motif2/

$sed '/^bonjour/,/^au revoir/d' test.txt → delete all lines between


'bonjour' and 'au revoir'.
1. Sed will repeat the command line until it finds another interval with
the specified motifs.

2. If only the /motif1/ sed will apply the command line in all lines
starting from '/motif1/'. Exemple: sed '/linux/,$d' sed.txt #pay
attention to $ before d.

3. We can use $sed '3,/^bonjour/ d' sed.txt to delete all lines from the
3rd line until the first line starting with 'bonjour'.

The p {print} command prints the selected line. The -n flag suppresses
automatic printing, so you need to use the p command to print the selected
line.

Examples ;

1. $sed -n 3p sed.txt → print only the third line.

2. $sed -n 3,6p sed.txt → print lines three through six.

3. $sed -n '/parler/p' sed.txt → print lines containing the word "parler".

4. $sed -n '/^tu /p' sed.txt → print lines that start with "tu".

The l {list} command and = ; the l command is used to display each line of
the file with non-printing characters visibly represented. It is often used
for debugging or for viewing special characters.

Examples ;

1. sed -n 'l' sed.txt is used to view the special characters in the file.

The = command is used to display line numbers alongside the content.

Examples ;

1. $sed '=' sed.txt

Output; 1

bonjour

Je suis ici en train de vous parler d'une commande très importante.

Elle s'appelle sed.

Elle aide à modifier un text sur Linux.

2. sed '/linux/=' sed.txt → it displays the line number of each occurrence


of the word "linux" in the file sed.txt.
The command d {delete};

Examples ;

1. $sed '4d ; 7d' sed.txt → This command deletes lines 4 and 7 from the
file sed.txt.

2. $sed '/^linux /d' sed.txt → This command deletes all lines that start
with the word "linux" from the file sed.txt.

3. $sed '/bonjour/ , /au revoir/d' sed.txt → This command deletes all


lines between the lines containing "bonjour" and "au revoir" in the
file sed.txt.

4. $sed '1d ; /^bonjour /d' sed.txt → This command deletes the first line
and any line that starts with "bonjour" in the file sed.txt.

The s {substitute} command ;

Exemples ;

1. $sed 's/bonjour/bonsoir/' sed.txt → substitutes the first occurrence of


the word "bonjour" with "bonsoir" in the file sed.txt.

2. $sed 's/bonjour/bonsoir/g' sed.txt → substitutes all occurrences of the


word "bonjour" with "bonsoir" in the file sed.txt.

3. $sed 's/bonjour/bonsoir/2' sed.txt → substitutes the second occurrence


of the word "bonjour" with "bonsoir" in the file sed.txt.

4. $sed -re 's/^# *//' sed.txt → removes all leading spaces and pound
signs from each line in the file sed.txt. * is a special character in
regular expressions that matches zero or more occurrences of the
preceding element.

5. $sed -re 's/ / /g' sed.txt → replaces all spaces with 6 additional
spaces in the file sed.txt.

6. $sed -z 's/\n/ /g' sed.txt → replaces all newlines with two additional
spaces in the file sed.txt. -z is used to change the line separator.

The y {transform};

y command is used to transform all instances of a character into another


character in the file sed.txt.

Exemple;

1. $sed -re 'y/éèê/eee/' sed.txt → changes all instances of the characters


é, è, and ê to e in the file sed.txt.

Add text with sed;

the command i {insert};

Exemples;
1. $sed '3i bonjouuuuuuur' sed.txt → inserts the text "bonjouuuuuuur" on
line 3 of the file sed.txt.

2. $sed '/linux /i my name is jooooohn cenaaa' sed.txt → inserts the text


"my name is jooooohn cenaaa" before any line containing "linux" in the
file sed.txt.

the command a {append};

Exemple;

1. $sed '8a hello' sed.txt → appends the text "hello" after line 8 in the
file sed.txt.

2. $sed '/linux/a My name is jooooohn cenaaa' sed.txt → appends the text


"my name is jooooohn cenaaa" after any line containing "linux" in the
file sed.txt.

the command c {change};

Exemple;

1. $sed '10c goodbye' sed.txt → changes the text on line 10 in the file
sed.txt to "goodbye".

2. $sed '/linux/c unix' sed.txt → changes any line containing "linux" in


the file sed.txt to "unix".

Address denial using ! . $sed '/linux/!c unix' sed.txt → changes any line
not containing "linux" in the file sed.txt to "unix".

Sed and substring;

sed 's/[0-9][0-9]*/number/g' sed.txt → changes any sequence of two or more


digits in the file sed.txt to "number".

q {quit} → exits the sed program.

r {read} → reads the contents of a file and appends them to the output
stream.

w {write} → writes the output stream to a file.

You might also like