/ factorpad.com / tech / linux-essentials / sed-command.html
An ad-free and cookie-free website.
Beginner
Learn to automate the editing of text files at the Linux command line.
Videos can also be accessed from the Linux Essentials Playlist on YouTube.
Linux sed Command Summary with Examples (3:38)
Our fortieth word, or command to memorize is
sed
from our category
Text.
sed
allows you to edit a stream of
text.
-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.
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
.
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
And then we'll cat
it here to verify
that it was done correctly.
And last, let's throw in the -n
option
with a /p
at the end, to print the
corrected line.
Again, sed
has a lot of functionality.
We're only touching the surface here.
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.
For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:
I suggest supplement the text here with videos on YouTube as two modes of learning will help this material stick.
grep
command,
hit Back.calendar
, click
Next./ factorpad.com / tech / linux-essentials / sed-command.html
A newly-updated free resource. Connect and refer a friend today.