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 ofsed
.'command'
: Thesed
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 containingsed
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:
-
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
-
Substitute (
s
): Replace text with another.sed 's/old_text/new_text/' file.txt
-
Delete (
d
): Delete lines.sed '2d' file.txt # Delete the second line sed '1,3d' file.txt # Delete lines 1 to 3
-
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
-
Append (
a
): Append text after a line.sed '2a Appended text' file.txt # Append after the second line
-
Print Line Numbers (
=
): Print line numbers.sed -n '=' file.txt
Examples of sed
Usage:
-
Basic Text Replacement:
- Replace "old" with "new" in a file.
sed 's/old/new/' file.txt
-
Printing Lines Containing a Pattern:
- Print lines containing the word "error."
sed -n '/error/p' file.txt
-
Deleting Lines Matching a Pattern:
- Delete lines containing "pattern."
sed '/pattern/d' file.txt
-
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
-
Print Line Numbers for All Lines:
- Print line numbers for all lines in a file.
sed '=' file.txt | sed 'N;s/\n/ /'
-
Removing Empty Lines:
- Remove empty lines from a file.
sed '/^$/d' file.txt
-
Formatting CSV Data:
- Convert CSV data with semicolons to tabs.
sed 's/;/\t/g' input.csv > output.tsv
-
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
- Add a comment (e.g.,
-
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
-
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.