Skip to content

Instantly share code, notes, and snippets.

@rpivo
Last active September 13, 2021 21:59
Show Gist options
  • Save rpivo/b745daf8b0c7c2838278dc43a7e41111 to your computer and use it in GitHub Desktop.
Save rpivo/b745daf8b0c7c2838278dc43a7e41111 to your computer and use it in GitHub Desktop.
The `sed` Command

The sed Command

The sed manual describes sed as a "stream editor" (stream editor). It is capable of filtering text and even has regex support.

Examples

As an example, we can create a mock file called example.txt and add text to it.

example.txt

This is one line of text.
Behold, a new line.
Once again, another line of text.
Even more text here.

If we simply try to invoke sed on this file, we'll get an error:

$ sed example.txt
sed: 1: "example.txt": invalid command code e

Find & Replace

You can find and replace in a file using sed like so:

$ sed 's/line/lawn/' example.txt
This is one lawn of text.
Behold, a new lawn.
Once again, another lawn of text.
Even more text here.

The -i flag will edit files rather than printing to the screen.

Related Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment