LinuxCommandLibrary

sed

Edit text by substitution

TLDR

Replace all apple (basic regex) occurrences with mango (basic regex) in all input lines and print the result to stdout

$ [command] | sed 's/apple/mango/g'
copy

Execute a specific script [f]ile and print the result to stdout
$ [command] | sed -f [path/to/script.sed]
copy

Print just a first line to stdout
$ [command] | sed -n '1p'
copy

SYNOPSIS

sed [OPTION]... {SCRIPT} [INPUT_FILE]...
sed [OPTION]... -e SCRIPT... -f SCRIPT_FILE... [INPUT_FILE]...

PARAMETERS

-n
    Suppress automatic printing of the pattern space. Only lines explicitly requested (e.g., with the 'p' command) are printed.

-e script
    Add script to the commands to be executed. Useful for specifying multiple editing commands directly on the command line.

-f script-file
    Add the contents of script-file to the commands to be executed. Allows complex sed scripts to be stored and reused from a file.

-i[SUFFIX]
    Edit files in place. If SUFFIX is provided, a backup of the original file is created with that suffix (e.g., '.bak'). Use with caution as it modifies the original file directly.

-E, -r
    Use extended regular expressions (EREs) instead of basic regular expressions (BREs). EREs offer more powerful pattern matching features, like '+' for one or more occurrences and '?' for zero or one.

-u
    Unbuffered processing. Data is flushed more frequently, which can be useful in pipelines where output is needed immediately.

--posix
    Disable all GNU extensions and use POSIX-compliant behavior, which can be useful for ensuring script portability across different sed implementations.

--version
    Output version information and exit.

--help
    Display a help message and exit.

DESCRIPTION

sed (stream editor) is a powerful, non-interactive command-line utility that performs text transformations on an input stream (typically a file or output from another command). It reads text line by line into a 'pattern space', applies a sequence of editing commands (defined in a 'sed script') to each line, and then outputs the modified line to standard output. sed is exceptionally versatile due to its robust support for regular expressions, enabling complex pattern matching and substitutions.

Common applications include search and replace operations, deleting or inserting lines, reformatting data, and extracting specific content. Unlike interactive text editors, sed processes data programmatically, making it an indispensable tool for scripting, automating repetitive text manipulation tasks, and pipeline processing in Unix-like environments. By default, it operates on a copy of the input, leaving the original file unchanged unless explicit in-place editing options are used.

CAVEATS

sed by default does not modify files in place; it prints results to standard output. The -i option is required for in-place editing, and caution should be exercised as it directly alters the file. Regular expression syntax can vary slightly between different sed implementations (e.g., GNU sed vs. BSD sed), particularly regarding extended regular expressions and certain escape sequences, which might lead to portability issues. While efficient for line-by-line processing, excessive use of hold space or very large pattern spaces for certain operations can consume significant memory.

COMMON COMMANDS AND SCRIPTING

sed operates by applying commands to lines matching an address (a line number or a regular expression). If no address is given, the command is applied to all lines.

Key commands include:
s/pattern/replacement/[flags]:
Substitute replacement for the first occurrence of pattern on the line. Common flags: g (global, all occurrences), i (case-insensitive), p (print pattern space if a substitution was made), w file (write to file).
d:
Delete the current line from the pattern space; prevents it from being printed.
p:
Print the current line in the pattern space. Often used with -n to selectively print lines.
a\text:
Append text after the current line.
i\text:
Insert text before the current line.
c\text:
Change (replace) the current line with text.

Addresses can be single (e.g., `/pattern/d` to delete lines matching pattern) or ranges (e.g., `start,endp` to print lines from start to end).

HISTORY

sed was developed by Lee E. McMahon of Bell Labs in 1973-1974 as one of the very early Unix tools. It emerged from the need for a non-interactive editor that could process text streams efficiently, particularly for automating tasks that were cumbersome with line editors like ed. Its design was influenced by ed's command set but adapted for stream processing. sed predates awk and Perl, filling a crucial gap for programmable text manipulation. Over the decades, it has remained a staple in the Unix toolkit, with GNU sed being the most prevalent implementation on Linux systems, incorporating various extensions and improvements over the original AT&T Unix version.

SEE ALSO

awk(1), grep(1), tr(1), cut(1)

Copied to clipboard