Sed (Stream Editor) #
Review
- 2019/12/25
Sed is a powerful stream editor that can perform basic text transformations on an input stream (a file or input from a pipeline). It is commonly used for text substitution, deletion, insertion, and other text manipulations.
Basic Usage #
sed [options] 'command' fileCommon Options #
-n: Suppress automatic printing of pattern space-e: Add the script to the commands to be executed-f: Add the contents of script-file to the commands to be executed-i: Edit files in place (make backup if extension supplied)-r: Use extended regular expressions
Basic Commands #
Substitution (s) #
# Basic substitution
sed 's/old/new/' file.txt
# Global substitution (replace all occurrences)
sed 's/old/new/g' file.txt
# Case-insensitive substitution
sed 's/old/new/i' file.txt
# Replace only the 2nd occurrence on each line
sed 's/old/new/2' file.txtDeletion (d) #
# Delete lines containing pattern
sed '/pattern/d' file.txt
# Delete lines 1-5
sed '1,5d' file.txt
# Delete last line
sed '$d' file.txtPrinting (p) #
# Print only lines matching pattern
sed -n '/pattern/p' file.txt
# Print lines 1-5
sed -n '1,5p' file.txtInsertion and Appending #
# Insert text before line 4
sed '4i\This is inserted text' file.txt
# Append text after line 4
sed '4a\This is appended text' file.txtAdvanced Usage #
Multiple Commands #
# Using -e for multiple commands
sed -e 's/old/new/' -e 's/another/other/' file.txt
# Using semicolon to separate commands
sed 's/old/new/; s/another/other/' file.txtAddress Ranges #
# Apply command to lines 1-5
sed '1,5s/old/new/' file.txt
# Apply command from line 10 to end
sed '10,$s/old/new/' file.txt
# Apply command to lines matching pattern
sed '/pattern/s/old/new/' file.txtHold Space and Pattern Space #
# Swap pattern space and hold space
sed 'x' file.txt
# Copy hold space to pattern space
sed 'g' file.txt
# Append pattern space to hold space
sed 'H' file.txtCommon Patterns #
Remove Empty Lines #
sed '/^$/d' file.txtRemove Leading/Trailing Whitespace #
# Remove leading whitespace
sed 's/^[ \t]*//' file.txt
# Remove trailing whitespace
sed 's/[ \t]*$//' file.txtConvert DOS Line Endings to Unix #
sed 's/\r$//' file.txtExtract Specific Lines #
# Extract lines 5-10
sed -n '5,10p' file.txt
# Extract lines containing pattern
sed -n '/pattern/p' file.txtmacOS Specific Notes #
On macOS, when using the -i option to edit files in place, you must specify an empty string as the backup extension:
sed -i '' 's/old/new/' file.txtBest Practices #
- Always test sed commands with
-nandpfirst to see what will be affected - Use
-iwith caution, especially on macOS - When using regular expressions, consider using
-rfor extended regex support - For complex transformations, consider using a sed script file with
-f - When working with large files, be mindful of performance implications
Common Pitfalls #
- Forgetting to escape special characters in patterns
- Not understanding the difference between
gandiflags - Misusing address ranges
- Not properly handling newlines in patterns
- Forgetting macOS requires empty string for
-ioption