FactorPad
Faster Learning Tutorials

Linux sed command summary with examples

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.
Paul Alan Davis, CFA, October 22, 2016
Updated: August 1, 2018
In this tutorial, 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 Next

~/ home  / tech  / linux essentials  / sed command


The Linux sed command

Beginner

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

Video Tutorial

Linux sed command summary with examples (3:38)

Videos can also be accessed from the YouTube Playlist.

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.

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?

Supplement the text here with videos. Subscribe here.

  • For the Outline to all 100 tutorials, click Outline.
  • To go back to the grep command, hit Back.
  • After a 10-tutorial set on text processing, we'll tackle an easy command next with calendar, click Next.

Outline Back Next

~/ home  / tech  / linux essentials  / sed command



 
 
Keywords:
linux sed command
sed linux
sed modify patterns
man sed
linux sed examples
sed replace text
sed options
sed regex
sed search and replace
sed replace string
linux sed summary
sed regular expressions
sed find text
linux sed tutorial
sed search files
sed multiple file search
sed search and replace
sed replace in file
sed syntax