FactorPad
Build a Better Process

Linux sed Command Summary with Examples

Linux sed allows you to edit a stream of text.
  1. Purpose - Learn what sed is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with sed.
  4. A tip - Finish off with one more insight.
face pic by Paul Alan Davis, CFA
Updated: February 23, 2021
In this tutorial on Linux sed, 40 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / sed-command.html


An ad-free and cookie-free website.


Examples of the Linux sed Command

Beginner

Learn to automate the editing of text files at the Linux command line.

Video Tutorial

Videos can also be accessed from the Linux Essentials Playlist on YouTube.

Linux sed Command Summary with Examples (3:38)

Video Script

The Command and Why You Need It

Our fortieth word, or command to memorize is sed from our category Text.

sed allows you to edit a stream of text.

Common Linux sed Options
-options description
--help Print help screen
-f SCRIPT Add SCRIPT of commands to be executed
/s/FIRST/SECOND Command to substitute FIRST word for SECOND

Recall from the last 10 videos, we sorted, cut, pasted, joined and searched text files, and now we'll conclude a 10-video set on text processing with a very brief introduction to the concept of automated editing.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The sed command has 13 traditional options, and the argument represents the file you'd like to review.

Like most commands, help is available with double-dash --help. And also make note, we're entering the realm of programming languages here, and sed like grep in the last video, and others we won't cover include: perl, ed, awk are very deep, so people only have time to master a few.

For example, sed offers the ability to write, and implement scripts using the -f option. And programs like sed have what I'll call sub-commands, /s is a very basic one here.

So why is sed an important command? Well, not only do we need to find patterns in text, but to automate those changes. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. Remember from the previous video on grep, we saw that we misspelled "third" as "thrid" in file video20.txt.

$ cat video20.txt # A file for video 20 on nano This is the first line This is the second line This is the thrid line This is the fourth line This is the fifth line

Try this, sed with the 's/thrid/third/' within single quotes on video20.txt, which is the argument, and we're going to write that to > video40.txt

$ sed 's/thrid/third/' video20.txt > video40.txt

And then we'll cat it here to verify that it was done correctly.

$ cat video40.txt # A file for video 20 on nano This is the first line This is the second line This is the third line This is the fourth line This is the fifth line

And last, let's throw in the -n option with a /p at the end, to print the corrected line.

$ sed -n 's/thrid/third/p' video20.txt This is the third line

Again, sed has a lot of functionality. We're only touching the surface here.

A Final Tip

Okay now you know how to use sed. And you know the syntax for commands, options and arguments.

One last tip about the sed command. So to fully explore its possibilities, you'll need a book dedicated to sed. It's that vast.

Okay, thanks for visiting today. I hope this was a helpful introduction to the sed command.


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

I suggest supplement the text here with videos on YouTube as two modes of learning will help this material stick.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / sed-command.html


linux sed command
sed linux
linux sed
unix sed
sed unix
linux sed examples
sed examples
man sed
sed command
sed replace
sed modify patterns
sed regular expression
sed options
sed syntax
sed replace text
linux sed summary
linux sed tutorial
sed examples
sed replace in file
sed search and replace
sed regular expression
sed regex

A newly-updated free resource. Connect and refer a friend today.