AI Generated Cheatsheets

sed Cheatsheet

Overview

sed (stream editor) is a Unix utility used to perform basic text transformations on an input stream (a file or input from a pipeline). It is a powerful tool for editing files in batch mode or in a script.

Usage

sed [options] [script] [input_file]

Options

Commands

Examples

Replace the first occurrence of “foo” with “bar” in a file:

sed 's/foo/bar/' input_file.txt

Replace all occurrences of “foo” with “bar” in a file:

sed 's/foo/bar/g' input_file.txt

Delete lines that contain the word “foo” in a file:

sed '/foo/d' input_file.txt

Insert a line before the first line of a file:

sed '1i This is the first line.' input_file.txt

Resources