2023-01-18
sed
sed
is one of the most powerful commands when it comes
to data wrangling in the shell. It’s called a “stream editor” because
you can use it to make edits to a “stream” of text, which could either
be a file or the output of another program.
The simplest way to use sed
is for finding a word and
replacing it with another word:
cat input.txt | sed 's/hello/hi/g'
Let’s look at this in detail:
sed
takes as its argument a “command string”, which
tells it what to dos
, the “substitute” command. This is the
general find-and-replace command, and is likely the most common one
you’ll use./
. This divider is used to separate the
different parts of the command string.hello
.hello
with
hi
.g
(global) flag.
This tells sed
to replace every instance of
hello
, not just the first one on each line.We can also use sed
with regular expressions, just like
we did with grep
in our previous
post.