Complete guide to Linux sed command with 20 practical examples covering substitution, deletion, transformations and advanced text processing
last modified April 2, 2025
The sed (stream editor) command is a powerful text processing tool in Linux that performs basic and advanced text transformations on an input stream. It reads text line by line, applies specified operations, and outputs the results. This guide covers sed fundamentals with 20 practical examples ranging from basic substitutions to advanced text manipulation. Mastering sed enables efficient batch editing of files and streams without manual intervention.
The basic syntax of sed is sed [options] ‘commands’ [input-file]. Sed operates on each line of input, applying one or more editing commands. By default, sed outputs to stdout without modifying the input file. This example shows fundamental sed usage patterns.
basic_syntax.sh
echo “hello world” | sed ’s/hello/hi/’
sed -i.bak ’s/foo/bar/’ file.txt
echo “hello world” | sed -e ’s/hello/hi/’ -e ’s/world/there/'
echo “path/to/file” | sed ’s|/|_|g'
seq 1 5 | sed -n ’s/3/three/p'
Example 1 shows basic substitution syntax s/pattern/replacement/. Example 2 demonstrates in-place file editing with backup. Example 3 combines multiple commands with -e. Example 4 uses alternative delimiters for readability. Example 5 shows how to suppress default output and print only modified lines.
Substitution is sed’s most common operation, using the s command. These examples demonstrate various substitution techniques with different flags and patterns. Mastering substitutions enables powerful text transformations.
substitution_examples.sh
echo “hello hello world” | sed ’s/hello/hi/g'
echo “one two two three” | sed ’s/two/2/2'
echo “Hello hello HELLO” | sed ’s/hello/hi/i'
echo “John Doe” | sed ’s/([^ ]) ([^ ])/\2, \1/'
echo “123 abc” | sed ’s/[0-9]*/& &/'
Example 6 shows global replacement with g flag. Example 7 replaces only the 2nd occurrence. Example 8 demonstrates case-insensitive matching with i. Example 9 uses grouping to reorder names. Example 10 shows how & represents the matched text.
Sed can select specific lines by number or pattern before applying operations. These examples demonstrate line addressing and deletion techniques. Precise line selection is essential for targeted text processing.
line_selection.sh
seq 1 5 | sed ‘/3/d’
seq 1 5 | sed ‘/3/!d’
seq 1 5 | sed ‘3d’
seq 1 5 | sed ‘2,4d’
seq 1 5 | sed ‘3,$d’
Example 11 deletes lines matching a pattern. Example 12 inverts the match with !. Example 13 deletes by line number. Example 14 shows range deletion. Example 15 deletes from line 3 to end ($).
Sed supports advanced text transformations including multi-line operations, hold space manipulation, and conditional execution. These examples demonstrate sed’s powerful editing capabilities for complex tasks.
advanced_operations.sh
echo -e “line1\nline2” | sed ‘/line1/a\appended text’
echo -e “line1\nline2” | sed ‘/line2/i\inserted text’
echo -e “old1\nold2” | sed ‘/old1/c\new line’
echo “hello” | sed ‘y/abcdefghij/0123456789/’
echo -e “line1\nline2\nline3” | sed ‘/line1/{N;s/line1\nline2/replaced/}’
Example 16 appends text after matches. Example 17 inserts before matches. Example 18 replaces entire lines. Example 19 transforms characters like tr. Example 20 demonstrates multi-line operations with N.
These practical examples solve real-world text processing problems using sed. They combine multiple techniques to demonstrate sed’s versatility in production scenarios.
practical_scripts.sh
echo -e “line1\n\nline2” | sed ‘/^$/d’
echo " text" | sed ’s/^[ \t]*//'
echo “text " | sed ’s/[ \t]*$//'
echo “<p>Hello <b>world</b></p>” | sed ’s/<[^>]*>//g'
echo -e “one\ntwo\nthree” | sed ‘=’ | sed ‘N;s/\n/ /’
Example 21 strips empty lines. Examples 22-23 trim whitespace. Example 24 removes HTML tags. Example 25 numbers lines by combining two sed commands.
Sed’s true power comes from combining its commands with regular expressions. These examples demonstrate sophisticated pattern matching and replacement techniques.
regex_examples.sh
echo “cart part smart” | sed ’s/\bpart\b/replace/g’
echo “before [extract] after” | sed ’s/.[([^]])].*/\1/'
sed ’s/\r$//’ dosfile.txt > unixfile.txt
echo “hello” | sed ’s/.*/\U&/'
echo “Call 1234567890” | sed ’s/([0-9]{3})([0-9]{3})([0-9]{4})/(\1) \2-\3/'
Example 26 matches whole words. Example 27 extracts text between brackets. Example 28 converts line endings. Example 29 uppercases text. Example 30 formats phone numbers with capture groups.
Sed provides branching commands for complex flow control in editing scripts. These examples demonstrate conditional operations and loops in sed.
branching_examples.sh
echo -e “include\nskip\ninclude” | sed ‘/skip/b; s/include/replaced/’
echo -e “line1\nspecial\nline2” | sed ‘/special/{s/line/xxx/;p;d}’
sed ‘/start/,/end/ s/foo/bar/’ file.txt
echo -e “a\nb\na\nc” | sed ‘:top; /a/{s/a/x/; b top}’
seq 1 10 | sed ‘/5/q’
Example 31 skips processing for matched lines. Example 32 applies multiple commands to matches. Example 33 shows range-based substitution. Example 34 demonstrates labels and branching. Example 35 quits after first match.
Sed maintains a pattern space (current line) and hold space (temporary storage) for advanced text manipulation. These examples demonstrate multi-line processing techniques.
hold_space_examples.sh
seq 1 3 | sed ‘1!G;h;$!d’
echo -e “a\nb\nc” | sed ‘G’
echo -e “a\nb\nc\nd” | sed ‘N;s/\n/ /’
echo -e “a\nb\na\nc” | sed -n ‘$!N; /^(.*)\n\1$/p; D’
echo -e “a\n\nb\nc” | sed ‘/./=’ | sed ‘/./N; s/\n/ /’
Example 36 reverses file lines. Example 37 double spaces lines. Example 38 joins line pairs. Example 39 finds duplicates. Example 40 numbers only non-empty lines. These demonstrate sed’s advanced buffer manipulation.
Test sed commands with sample input before applying to important files. Use -e for complex scripts to improve readability. Precede in-place edits (-i) with backups during development. Comment complex sed scripts with # for future maintenance. Consider using perl or awk for extremely complex text processing tasks.
Learn more from these resources: GNU sed Manual, sed Man Page, and sed & awk Book.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Linux tutorials.