Linux - Sed (Stream editor)

> Procedural Languages > Bash Shell and (Unix|Linux) Utilities (XCU)

1 - About

sed stands for stream editor and is used for filtering and transforming text

The sed command within a file can:

It :

Advertising

3 - Syntax

# Default
sed 'expression1;...;expressionN' inputFileName > outputFileName
# In place editing - No outputFileName needed
sed -i 'expression1;...;expressionN' inputFileName

where:

  • expression
command/regularExpression/modifier

4 - Script

Using a script file avoids problems with shell escaping or substitutions.

Example script.sed: A sed file script with one command by line and a shebang

script.sed
#!/bin/sed -f
sedExpression1
sedExpression...
sedExpressionN

Run it:

  • with the f option
sed -f script.sed inputFileName > outputFileName
chmod u+x subst.sed
script.sed inputFileName > outputFileName
Advertising

5 - Command

5.1 - Substitution

The Substitution command replace a string

# First occurence Default
sed 's/searchString/replacementString/' inputFileName > outputFileName
# All Occurences thanks to the g at the end
sed 's/searchString/replacementString/g' inputFileName > outputFileName
# In place editing - No outputFileName needed
sed -i 's/searchString/replacementString/g' inputFileName

where: in the expression 's/searchString/replacementString/':

  • s stands for “substitution”.
  • searchString: the search string, the text to find.
  • replacementString: the replacement string
  • g stands for global (ie replace all occurence)
  • i is an option to edit the file directly - no need of outputFileName (a temporary output file is created in the background)

5.2 - Delete

The d (delete) command delete lines (to delete a word, substitute it with nothing)

# line
sed '/regularExpression/d' inputFileName
# word

Example:

  • delete lines that are either blank or only contain spaces
sed '/^ *$/d' inputFileName
  • delete word (ie substitute with empty)
s/yourword//g
Advertising

5.3 - Others

  • N add the next line to the pattern space;
  • P print the top line of the pattern space;
  • D delete the top line from the pattern space and run the script again.

6 - Flow

Flow of control can be managed by:

  • the use of a label (a colon followed by a string)
  • and the branch instruction b.

An instruction b followed by a valid label name will move processing to the block following that label.

7 - Documentation / Reference