The 'sed' Command in Unix/Linux

Master the 'sed' command for text editing in Unix/Linux. Our guide covers 'sed' syntax, regular expressions, and practical usage for efficient text manipulation.
E
Edtoks4:20 min read

The sed command, short for "stream editor," is a powerful text-processing utility in Unix-like operating systems. It allows you to perform various text transformations on input text (or files) and produce output based on specified commands. sed uses a simple scripting language that includes commands for searching, replacing, inserting, deleting, and more. Let's explore sed in detail with examples:

Basic Syntax:

sed [options] 'command' input_file
  • options: Optional flags to modify the behavior of sed.
  • 'command': The sed command enclosed in single quotes.
  • input_file: The input file to process. If omitted, sed reads from standard input.

Common Options:

  • -n or --quiet or --silent: Suppress automatic output of pattern space.
  • -e 'command' or --expression='command': Add a script of editing commands.
  • -f script_file: Specify a file containing sed commands.
  • -i or --in-place: Edit files in place (overwrites the original file).

sed Commands:

sed commands consist of an address (optional) and a command. The address specifies the line(s) to which the command should be applied. If no address is specified, the command is applied to all lines.

Here are some common sed commands:

  1. Print (p): Print the current pattern space (line).

    sed -n '1p' file.txt   # Print the first line
    sed -n '1,3p' file.txt # Print lines 1 to 3
    
  2. Substitute (s): Replace text with another.

    sed 's/old_text/new_text/' file.txt
    
  3. Delete (d): Delete lines.

    sed '2d' file.txt      # Delete the second line
    sed '1,3d' file.txt    # Delete lines 1 to 3
    
  4. Insert (i): Insert text before a line.

    sed '2i New line' file.txt      # Insert before the second line
    sed '1i First line\nSecond line' file.txt  # Insert multiple lines
    
  5. Append (a): Append text after a line.

    sed '2a Appended text' file.txt   # Append after the second line
    
  6. Print Line Numbers (=): Print line numbers.

    sed -n '=' file.txt
    

Examples of sed Usage:

  1. Basic Text Replacement:

    • Replace "old" with "new" in a file.
    sed 's/old/new/' file.txt
    
  2. Printing Lines Containing a Pattern:

    • Print lines containing the word "error."
    sed -n '/error/p' file.txt
    
  3. Deleting Lines Matching a Pattern:

    • Delete lines containing "pattern."
    sed '/pattern/d' file.txt
    
  4. In-Place Editing (Replacing in a File):

    • Replace "Unix" with "Linux" in a file, and edit the file in place.
    sed -i 's/Unix/Linux/' file.txt
    
  5. Print Line Numbers for All Lines:

    • Print line numbers for all lines in a file.
    sed '=' file.txt | sed 'N;s/\n/ /'
    
  6. Removing Empty Lines:

    • Remove empty lines from a file.
    sed '/^$/d' file.txt
    
  7. Formatting CSV Data:

    • Convert CSV data with semicolons to tabs.
    sed 's/;/\t/g' input.csv > output.tsv
    
  8. Adding Comments to a Configuration File:

    • Add a comment (e.g., # Comment) before each line in a configuration file.
    sed 's/^/# Comment\n/' config.txt > new_config.txt
    
  9. Extracting Data:

    • Extract email addresses from a file using a regular expression.
    sed -nE 's/.*([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}).*/\1/p' file.txt
    
  10. Deleting HTML Tags:

    • Remove HTML tags from an HTML file.
    sed 's/<[^>]*>//g' file.html
    

These sed examples illustrate some of the basic and common tasks you can perform using sed. The power of sed lies in its scripting capabilities, allowing you to create complex editing commands and automate text-processing tasks efficiently.

Let's keep in touch!

Subscribe to keep up with latest updates. We promise not to spam you.