25 Linux 'sed' Command Examples

1. Substitute a String in a Line

sed 's/dog/cat/' file.txt

Replaces the first occurrence of "dog" with "cat" in each line.

2. Replace All Occurrences in a File

sed 's/dog/cat/g' file.txt

g replaces all instances of "dog" in each line.

3. Delete a Specific Line

sed '3d' file.txt

Deletes the 3rd line of the file.

4. Delete Lines Matching a Pattern

sed '/error/d' log.txt

Removes all lines containing the word "error".

5. Append Text After a Line

sed '/START/a New text line' file.txt

Adds "New text line" after lines matching "START".

6. Insert Text Before a Line

sed '/END/i Inserted line' file.txt

Inserts "Inserted line" before lines with "END".

7. Print Lines Containing a Pattern

sed -n '/warning/p' log.txt

-n suppresses output except for matching lines.

8. Use Regular Expressions for Substitution

sed 's/[0-9][0-9]/XX/g' file.txt

Replaces all two-digit numbers with "XX".

9. Replace Multiple Instances in a Line

sed 's/old/new/g' file.txt

Same as Example 2, but emphasized for clarity.

10. Delete Blank Lines

sed '/^$/d' file.txt

Removes empty lines (^ matches start, $ end of line).

11. Replace Tabs with Spaces

sed 's/\t/ /g' file.txt

Replaces each tab with four spaces.

12. Reverse Lines in a File

sed '1!G;h;$!d' file.txt

A classic trick to reverse the order of lines.

13. Count Lines in a File

sed -n '$=' file.txt

Returns the total number of lines (alternative to `wc -l`).

14. Add Line Numbers

sed '=' file.txt | sed 'N;s/\n/ /'

Numbers each line (requires two passes).

15. Swap Two Words in a Line

sed 's/\(.*\) \(.*\)/\2 \1/' file.txt

Swaps the first and second words (uses backreferences).

16. Extract Specific Columns

sed 's/[^ ]*//3' file.txt

Extracts the third space-separated column.

17. Use Hold Space for Multi-Line Edits

sed -e '1h;1d' -e '$!H;$!d' -e 'g' file.txt

Swaps first and last lines (advanced example).

18. In-Place Editing with Backup

sed -i.bak 's/error/warning/g' file.txt

Modifies the file and saves a backup as `file.txt.bak`.

19. Multiple Substitutions in One Command

sed 's/old/new/; s/error/warning/' file.txt

Performs two substitutions sequentially.

20. Extended Regular Expressions

sed -E 's/(old|existing)/new/g' file.txt

Uses `-E` for more readable regex (e.g., `|` for OR).

21. Delete Everything After a Pattern

sed 's/\(.*warning\).*/\1/' file.txt

Truncates lines at "warning" (keeps everything before).

22. Replace the Third Occurrence

sed 's/dog/cat/3' file.txt

Replaces only the third "dog" in each line.

23. Use Variables in sed

sed "s/old/$NEW_VAR/g" file.txt

Substitutes using shell variables (e.g., `NEW_VAR="new"`).

24. Edit XML/HTML Tags

sed 's/<\/div>/\n<\/div>/g' file.html

Adds a newline before closing `</div>` tags.

25. Conditional Processing with Branches

sed '/start/{:a;N;/end/b;p;d;ba}' file.txt

Prints only text between "start" and "end" markers.

Quick Reference

Command Description
s/pattern/replacement/ Substitute occurrences of pattern.
d Delete lines matching criteria.
i and a Insert or append text before/after lines.
-i Edit files in-place (add .bak for backups).
-n Suppress automatic printing (use p explicitly).

Key Notes: