Sed Command - Stream Editor
The sed command, short for stream editor, is a powerful utility for performing basic text transformations on an input stream (a file or input from a pipeline). It is widely used in shell scripting and command-line operations for tasks such as searching, replacing, inserting, and deleting text.
Sed Command Usage Examples
Basic Substitution
Previewing a file edit via substitution. This command replaces the first occurrence of 'Xfce Session' with 'Xfce_Session' in each line of 'FILE' without modifying the file itself.
# Preview a file edit, via substitution.
sed 's/Name=Xfce Session/Name=Xfce_Session/' FILE
Global Substitution
Replacing all occurrences of a string on a line. The 'g' flag ensures that all instances of 'Name=Xfce Session' are replaced with 'Name=Xfce_Session' on each line.
# Replace the same string more than once per line (g flag)
sed 's/Name=Xfce Session/Name=Xfce_Session/g' FILE
In-Place Editing
Modifying a file directly. The -i flag applies the changes made by sed directly to the specified file.
# Edit a file (adding -i flag), in-place; changes are made to the file(s).
sed -i 's/Name=Xfce Session/Name=Xfce_Session/' FILE
Handling Special Characters and Delimiters
Escaping special characters within the search or replacement string is crucial. Alternatively, using a different delimiter like a pipe (|) can simplify commands involving paths with slashes.
# It can become necessary to escape special characters in your string.
sed -i 's/\/path\/to\/somewhere\//\/path\/to\/anotherplace\//' FILE
# Change your sed delimiter to a pipe to avoid escaping slashes.
sed -i 's|/path/to/somewhere/|/path/to/anotherplace/|' FILE
Printing Specific Lines
sed can be used to print specific lines or ranges of lines from a file. The -n option suppresses default output, and p is used to print selected lines.
# Print 2nd line
sed -n '2p' FILE
# Print lines from 2 till 9
sed -n '2,9p' FILE
# Print lines starting from one having pattern "any" till line number 17
sed -n '/any/,17p' FILE
# Print lines starting from the beginning, quit after printing 3rd line
sed -n 'p;3q'
# Print and quit at 5th line
sed -n '5{p;q}' FILE
# Print lines starting from the one having pattern "strstart" till
# the line having pattern "strend"
sed -n '/strstart/,/strend/p' FILE
# Print the last line
sed -n '$p' FILE
Text Formatting and Cleaning
sed is excellent for cleaning up text files, such as replacing tabs with spaces, converting line endings, removing trailing whitespace, and deleting empty lines.
# Replace tabs with 4 spaces (changes are written to file itself)
sed -i 's/\t/ /g' FILE
# Replace CRLF with LF (convert DOS/Windows line endings to Linux line endings)
sed -i 's/\r$//g' FILE
# Insert CR (carriage return) character before LF (line feed) character
# (Linux to DOS/Windows line endings conversion)
sed -i 's/$/\r/' FILE
# Remove trailing spaces (changes are written to file itself)
sed -i -E "s/\s+$//g" FILE
# Remove empty lines (changes are written to file itself)
sed -i -E "/^\s*$/d" FILE
Advanced Examples
sed can be combined with other commands and used for more complex pattern matching and data manipulation.
# Anonymize original MAC address of an Ethernet device in ifconfig's output
# (match 6 pairs of hexadecimal numbers with an optional trailing ":")
ifconfig | sed -E "/ether/ s/([0-9a-f]{2}:{0,1}){6}/00:00:00:00:00:00/g"
# Rearrange order of %Y,%m,%d in `date`s output by matching groups of characters
date '+%Y-%m-%d' | sed -E "s/([0-9]{4})-([0-9]{2})-([0-9]{2})/\3-\2-\1/"