Table of Contents

  1. sed Command Guide
    1. Overview
    2. Basic Syntax
    3. Basic Examples
      1. Print Specific Lines
      2. Replace Text
      3. Delete Lines
      4. Insert / Append Text
      5. Modify Specific Lines
    4. Using sed with Pipelines
      1. Replace Text from stdin
      2. Remove Blank Lines
      3. Number All Lines
    5. Regular Expressions in sed
    6. Advanced Examples
      1. Replace Only on Specific Line Range
      2. Change Text Between Patterns
      3. Print Lines Not Matching a Pattern
      4. Replace Using Variables in Shell
      5. Remove Comments and Blank Lines
      6. Replace Tabs with Spaces
    7. Tips and Tricks
      1. 1. Backup Before Editing In-Place
      2. 2. Chain Multiple Commands
      3. 3. Read from File with Commands
      4. 4. Use & to Reuse the Matched Pattern
      5. 5. Debug Complex Expressions
      6. 6. Replace with Newline Characters
      7. 7. Swap Two Words
      8. 8. Extract Text Between Delimiters
      9. 9. Replace nth Occurrence in Line
      10. 10. Work with Multiline Patterns (GNU sed only)
    8. Common Use Cases
    9. One-Liners
      1. 1. Enable root login with password in sshd_config
      2. 2. Revert changes
    10. References

sed Command Guide

Overview

sed (Stream Editor) is a powerful Unix utility for parsing and transforming text using simple commands and regular expressions. It is commonly used for tasks such as search-and-replace, line deletion, and text substitution in scripts or pipelines.


Basic Syntax

sed [options] 'command' file

Common options: | Option | Description | |———|————-| | -n | Suppress automatic printing of pattern space | | -e | Add script to execute | | -i | Edit file in place | | -r | Use extended regular expressions | | -f | Read sed commands from a file |


Basic Examples

sed -n '5p' file.txt        # Print line 5
sed -n '1,3p' file.txt      # Print lines 1 to 3
sed -n '/pattern/p' file.txt  # Print lines matching pattern

Replace Text

sed 's/foo/bar/' file.txt             # Replace first occurrence of 'foo' with 'bar'
sed 's/foo/bar/g' file.txt            # Replace all occurrences on each line
sed -i 's/foo/bar/g' file.txt         # Replace in file directly
sed 's/[0-9]//g' file.txt             # Remove all digits

Delete Lines

sed '1d' file.txt             # Delete first line
sed '1,5d' file.txt           # Delete lines 1 through 5
sed '/pattern/d' file.txt     # Delete lines matching pattern

Insert / Append Text

sed '2i\Inserted before line 2' file.txt    # Insert before line 2
sed '3a\Appended after line 3' file.txt     # Append after line 3

Modify Specific Lines

sed '5s/old/new/' file.txt      # Replace only on line 5
sed '/error/s/fail/pass/g' file.txt  # Replace text only in lines containing 'error'

Using sed with Pipelines

Replace Text from stdin

echo "hello world" | sed 's/world/sed/'

Remove Blank Lines

cat file.txt | sed '/^$/d'

Number All Lines

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

Regular Expressions in sed

  • . — Matches any character
  • ^ — Start of line
  • $ — End of line
  • * — Zero or more occurrences
  • \{n,m\} — Repetition count
  • [] — Character class
  • () — Grouping (use -r or -E for extended)
  • | — Alternation (OR) in extended regex

Example:

sed -r 's/(cat|dog)/animal/g' file.txt

Advanced Examples

Replace Only on Specific Line Range

sed '10,20s/error/fixed/g' log.txt

Change Text Between Patterns

sed '/BEGIN/,/END/s/foo/bar/g' file.txt
sed -n '/pattern/!p' file.txt

Replace Using Variables in Shell

search="foo"
replace="bar"
sed -i "s/${search}/${replace}/g" file.txt

Remove Comments and Blank Lines

sed '/^#/d;/^$/d' config.txt

Replace Tabs with Spaces

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

Tips and Tricks

1. Backup Before Editing In-Place

sed -i.bak 's/foo/bar/g' file.txt

2. Chain Multiple Commands

sed -e 's/foo/bar/' -e '/baz/d' file.txt

3. Read from File with Commands

sed -f script.sed file.txt

4. Use & to Reuse the Matched Pattern

echo "error" | sed 's/.*/(&)/'     # Output: (error)

5. Debug Complex Expressions

sed -n 'l' file.txt

6. Replace with Newline Characters

sed 's/;/\n/g' file.txt

7. Swap Two Words

echo "foo bar" | sed 's/\(.*\) \(.*\)/\2 \1/'

8. Extract Text Between Delimiters

echo "<tag>value</tag>" | sed -n 's/.*<tag>\(.*\)<\/tag>.*/\1/p'

9. Replace nth Occurrence in Line

sed 's/foo/bar/2' file.txt   # Replace 2nd occurrence only

10. Work with Multiline Patterns (GNU sed only)

sed ':a;N;$!ba;s/old/new/g' file.txt

Common Use Cases

Task Command
Remove leading whitespace sed 's/^\s*//' file.txt
Remove trailing whitespace sed 's/\s*$//' file.txt
Double-space file sed G file.txt
Print only unique lines sed '$!N; /\n.*$/!p; D' file.txt
Convert lowercase to uppercase sed 's/[a-z]/\U&/g' file.txt
Convert uppercase to lowercase sed 's/[A-Z]/\L&/g' file.txt

One-Liners

1. Enable root login with password in sshd_config

-i.bak creates backup with .bak file ext in the location where the originating file resides

sed -i.bak 's/^#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config 

2. Revert changes

replace existing config with .bak file backup or run sed command below

cp /etc/ssh/sshd_config.bak /etc/ssh/sshd_config && systemctl restart sshd
sed -i.bak 's/^PermitRootLogin yes/PermitRootLogin prohibit-password/' /etc/ssh/sshd_config && systemctl restart sshd

Then verify

grep -E "PermitRootLogin|Password" /etc/ssh/sshd_config

References