Home > Text Processing & Search > sed

sed: Stream Editor

sed (stream editor) is a powerful Unix utility for editing text streams. It performs transformations, filtering, deletion, insertion, and other text manipulations on input from files or pipes using regular expressions. Optimized for non-interactive editing, it's widely used for automated text processing in scripts.

Overview

sed reads input streams (files or standard input) line by line, applies specified scripts (commands), and outputs the results to standard output. It is primarily used for modifying text file content, searching and replacing specific patterns, and deleting lines.

Key Features

  • Powerful pattern matching using regular expressions
  • Text substitution
  • Line deletion
  • Line insertion and appending
  • In-place file editing (-i option)

Key Options

The main options for the sed command control script specification and output behavior.

Script and Behavior Control

Generated command:

Try combining the commands.

Description:

`sed` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Learn text processing methods through various examples of sed usage.

Substitute Specific String

sed 's/old_text/new_text/g' filename.txt

Replaces all occurrences of 'old_text' with 'new_text' in a file. The 'g' flag substitutes all matches on a single line.

Delete Lines Containing Specific Pattern

sed '/error/d' logfile.txt

Deletes all lines that contain the word 'error'.

Edit File Content In-Place

sed -i.bak 's/DEBUG=true/DEBUG=false/g' config.txt

Changes 'DEBUG=true' to 'DEBUG=false' in the original 'config.txt' file and creates a backup file with a '.bak' extension.

Print Only the First 5 Lines

sed -n '1,5p' data.txt

Prints only the first 5 lines of a file. Uses the '-n' option and the '1,5p' command.

Delete Blank Lines

sed '/^$/d' document.txt

Deletes completely empty lines from a file.

Tips & Precautions

Points to note and tips for efficient use of sed.

Misconception about sed -d Option

Regarding the provided 'sed-d' hint, `-d` is not a valid command-line option for `sed`. `sed` commands use options like `-e`, `-f`, `-i`, `-n`, etc. If you execute `sed -d`, `sed` might interpret `-d` as a filename or raise an unknown option error.

Correct Usage of the 'd' Command

`d` is a command used within a `sed` script to delete specific lines. For example, `sed '/pattern/d' filename` deletes all lines containing 'pattern'. This is a completely different concept from a command-line option `-d`.

Example of 'd' Command Usage

sed '/string_to_delete/d' input.txt > output.txt

Deletes lines containing 'string_to_delete' from a file.

Leveraging Regular Expressions

The power of sed comes from regular expressions. Mastering various metacharacters (e.g., `^`, `$`, `*`, `+`, `?`, `.` etc.) allows for complex pattern matching and substitution.

  • `^`: Start of line
  • `$`: End of line
  • `.`: Any single character
  • `*`: Zero or more occurrences of the preceding character
  • `s/old/new/g`: Substitute all 'old' with 'new'
  • `/pattern/d`: Delete lines containing 'pattern'

Habitually Create Backup Files

When editing files directly with the `-i` option, it is always recommended to specify a backup suffix to prevent data loss (e.g., `sed -i.bak ...`).



Same category commands