sed stands for stream editor.
It is a filter program used for filtering and transforming text
It:
In the stream, it can:
It's part of the Gnu utility.
Sed is line-based therefore it is hard for it to grasp newlines and to manipulate eol characters.
Use the utility:
# Default
sed 'expression1;...;expressionN' inputFileName > outputFileName
# In place editing - No outputFileName needed
sed -i 'expression1;...;expressionN' inputFileName
where:
command/regularExpression/modifier
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
#!/bin/sed -f
sedExpression1
sedExpression...
sedExpressionN
Run it:
sed -f script.sed inputFileName > outputFileName
chmod u+x subst.sed
script.sed inputFileName > outputFileName
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
# to use backslash characters. tab by arrow and end of line by reverse p
sed 's/\t/→/g;s/$/¶'
where: in the expression 's/searchString/replacementString/':
The d (delete) command delete lines (to delete a word, substitute it with nothing)
# line
sed '/regularExpression/d' inputFileName
# word
Example:
sed '/^ *$/d' inputFileName
s/yourword//g
Flow of control can be managed by:
An instruction b followed by a valid label name will move processing to the block following that label.