# Cheatsheet & Examples: sed

The `sed` command is a stream editor used to perform basic text transformations on an input stream (like a file or input from a pipeline). It can search, replace, delete, insert, and manipulate text based on patterns or line numbers.

## Replace a specific pattern in a file

Example Usage:
`sed 's/pattern/replacement/' file.txt`

What it does:
Replaces the first occurrence of `pattern` with `replacement` on each line of the input file.

Command-line Arguments Explained:
- `s`: The substitute command, which replaces text.
- `pattern`: The text or regular expression to search for.
- `replacement`: The text to replace the matched pattern.

## Delete lines matching a pattern

Example Usage:
`sed '/pattern/d' file.txt`

What it does:
Deletes all lines that match the specified `pattern`.

Command-line Arguments Explained:
- `/pattern/`: The text or regular expression to search for.
- `d`: The delete command.

## Insert text before a matching line

Example Usage:
`sed '/pattern/i\New line' file.txt`

What it does:
Inserts a new line `New line` immediately before every line that matches `pattern`.

Command-line Arguments Explained:
- `/pattern/`: The text or regular expression to search for.
- `i\`: The insert command, followed by the text to add.

## Append text after a matching line

Example Usage:
`sed '/pattern/a\New line' file.txt`

What it does:
Appends a new line `New line` immediately after every line that matches `pattern`.

Command-line Arguments Explained:
- `/pattern/`: The text or regular expression to search for.
- `a\`: The append command, followed by the text to add.

## Substitute all occurrences in a line

Example Usage:
`sed 's/pattern/replacement/g' file.txt`

What it does:
Replaces all occurrences of `pattern` with `replacement` on each line of the input file.

Command-line Arguments Explained:
- `s`: The substitute command.
- `g`: The global flag, which ensures all matches in the line are replaced.

## Print specific lines

Example Usage:
`sed -n '3p' file.txt`

What it does:
Prints only the third line of the input file (suppresses default output).

Command-line Arguments Explained:
- `-n`: Disables default output (quiet mode).
- `3p`: The address `3` and the print command `p`.

## In-place editing of a file

Example Usage:
`sed -i 's/foo/bar/' file.txt`

What it does:
Modifies the file `file.txt` in-place, replacing `foo` with `bar` on each line.

Command-line Arguments Explained:
- `-i`: Edits the file directly (in-place).
- `s/foo/bar/`: The substitute command.

## Delete blank lines

Example Usage:
`sed '/^$/d' file.txt`

What it does:
Removes empty lines from the input file.

Command-line Arguments Explained:
- `/^$/`: Regular expression matching empty lines.
- `d`: Delete command.

## Apply multiple commands

Example Usage:
`sed -e 's/foo/bar/' -e 's/one/two/' file.txt`

What it does:
Applies multiple substitution commands to the input file.

Command-line Arguments Explained:
- `-e`: Specifies an additional script to apply.
- `s/foo/bar/`: First substitution command.
- `s/one/two/`: Second substitution command.

## Print lines containing a specific pattern

Example Usage:
`sed -n '/error/p' file.txt`

What it does:
Prints only the lines that contain the word `error`.

Command-line Arguments Explained:
- `-n`: Suppresses default output.
- `/error/`: The pattern to match.
- `p`: Print command.

## Delete all lines except those matching a pattern

Example Usage:
`sed '/pattern/!d' file.txt`

What it does:
Deletes all lines that do not match the specified `pattern`.

Command-line Arguments Explained:
- `/pattern/`: The address for lines to keep.
- `!`: Inverts the address match.
- `d`: Delete command.

## Quit after a specific line

Example Usage:
`sed '/pattern/q' file.txt`

What it does:
Exits immediately after the first line matching `pattern` is found.

Command-line Arguments Explained:
- `/pattern/`: The text or regular expression to search for.
- `q`: Quit command.

## Substitute using backreferences

Example Usage:
`sed 's/\(hello\) world/\1/' file.txt`

What it does:
Replaces `hello world` with `hello`, using a backreference to capture groups.

Command-line Arguments Explained:
- `s`: Substitute command.
- `\(hello\)` and `\1`: Capture group and backreference in the pattern.
- `world`: The text to replace.

## Escape special characters in replacement

Example Usage:
`sed 's/\/pattern/\/replacement/' file.txt`

What it does:
Replaces `/pattern` with `/replacement` in the input file, properly escaping the slashes.

Command-line Arguments Explained:
- `s`: Substitute command.
- `\/pattern` and `\/replacement`: Escaped slashes in the pattern and replacement.

## Substitute with case-insensitive matching

Example Usage:
`sed 's/pattern/replacement/i' file.txt`

What it does:
Replaces `pattern` with `replacement`, ignoring case in the match.

Command-line Arguments Explained:
- `s`: Substitute command.
- `i`: Case-insensitive flag for the substitution.

## Print line numbers with content

Example Usage:
`sed '=' file.txt`

What it does:
Prints line numbers before each line of the input file.

Command-line Arguments Explained:
- `=`: Print the line number of each line.

## Replace text in a range of lines

Example Usage:
`sed '3,5s/foo/bar/' file.txt`

What it does:
Replaces `foo` with `bar` in lines 3 to 5 of the input file.

Command-line Arguments Explained:
- `3,5`: The range of lines to apply the command to.
- `s/foo/bar/`: The substitute command.

## Use a script file

Example Usage:
`sed -f script.sed file.txt`

What it does:
Reads and executes commands from the script file `script.sed`.

Command-line Arguments Explained:
- `-f`: Specifies the script file to use.
- `script.sed`: The file containing `sed` commands.
- `file.txt`: The input file to process.

## Substitute using a new line character

Example Usage:
`sed 's/pattern/replacement\n/' file.txt`

What it does:
Replaces `pattern` with `replacement` followed by a newline.

Command-line Arguments Explained:
- `s`: Substitute command.
- `replacement\n`: Includes a newline in the replacement text.
