Understanding the `sed` - Stream Editor
The sed
command in Linux stands for "stream editor" and is used to perform
basic text transformations on an input stream (a file or input from a pipeline).
It's an incredibly powerful tool for text processing.
Syntax of sed
The basic syntax of sed
is:
sed [OPTIONS]... {script} [input-file]...
Here, the script
is a set of commands that sed
will execute,
and input-file
is the file that sed
will read.
Understanding sed
Command Syntax
The sed
script syntax, like s/sed/SED/
, follows a pattern that is common for
many command-line text processing utilities. Understanding how to
construct sed
scripts is critical for users to apply their own transformations
to text data effectively.
A sed
script is a set of commands passed to sed
to perform on the text. The
most common command is s
, for substitution. The syntax for a substitution
command in sed
is:
s/PATTERN/REPLACEMENT/FLAGS
s
— Indicates a substitution operation.PATTERN
— The regular expression pattern to search for.REPLACEMENT
— The text to replace the matched pattern.FLAGS
— Optional flags that alter the behavior of the substitution. For example,g
for global replacement.
Available Operartors For Script Patterns
Certainly! Below is a table summarizing various sed
operations, their
descriptions, and example scripts for each.
Operator | Description | Example Script | Explanation |
---|---|---|---|
s | Substitutes the first occurrence of a pattern within a line. | sed 's/foo/bar/' | Replaces the first instance of foo with bar in each line. |
g | When used with s , it substitutes all occurrences within a line (global replacement). | sed 's/foo/bar/g' | Replaces all instances of foo with bar in each line. |
p | Prints the pattern space (normally, sed only outputs the changed lines). Used with -n to print changed lines. | sed -n 's/foo/bar/p' | Replaces the first instance of foo with bar and prints only the lines where substitution has occurred. |
d | Deletes lines or a range of lines. | sed '/foo/d' | Deletes all lines containing foo . |
i | Inserts text before a line. | sed '/foo/i\bar' | Inserts the text bar before each line containing foo . |
a | Appends text after a line. | sed '/foo/a\bar' | Appends the text bar after each line containing foo . |
c | Changes lines matching a pattern to new text. | sed '/foo/c\bar' | Changes lines containing foo to bar . |
y | Transforms characters (like tr in shell). | sed 'y/foo/bar/' | Translates each character in foo to the corresponding character in bar . |
n | Reads the next line of input into the pattern space. | sed '/foo/{n;s/foo/bar/;}' | If a line contains foo , it moves to the next line and then replaces foo with bar in that line. |
q | Quits sed after processing the specified number of lines. | sed '10q' | Quits sed after processing the first 10 lines. |
= | Prints the current line number. | sed '/foo/=' | Prints the line numbers of lines containing foo . |
r | Reads a file into the buffer and appends it to the pattern space when a match is found. | sed '/foo/r bar.txt' | Appends the contents of bar.txt after lines containing foo . |
w | Writes the pattern space to a file. | sed -n '/foo/w bar.txt' | Writes only the lines containing foo to bar.txt . |
{} | Groups several commands to be executed sequentially. | sed '/foo/{s/foo/bar/;s/baz/qux/;}' | On lines containing foo , replaces foo with bar , and then replaces baz with qux . |
! | Applies the command to all lines that do not match the pattern. | sed '/foo/!s/bar/baz/' | On lines that do not contain foo , replaces bar with baz . |
These are some of the most commonly used sed
operations. The sed
command is
extremely powerful and has many more options and intricacies that you can
explore in the sed
man pages or detailed sed
tutorials. The sed
manual can
be accessed on the command line using man sed
.
How to Use sed
Scripts:
To use these
sed
scripts, you typically echo a string or pipe a file intosed
. For example:echo "This is foo with baz" | sed 's/foo/bar/g'
This will output the string "This is bar with baz" as the script replaces " foo" with "bar" globally on each line.
To apply
sed
to a file, you can redirect a file into it or specify the filename at the end of thesed
command. For example:sed 's/foo/bar/g' filename.txt
To save the changes back into the file, you can use the
-i
option to edit the file in-place. Note that-i
might not be supported in some versions of sed
, or it might require an empty
string argument (-i ''
) on BSD systems, including macOS.
sed -i 's/foo/bar/g' filename.txt
Commonly Used Options in sed
Here is a table of commonly used options with sed
:
Option | Shorthand | Description |
---|---|---|
--version | Display the version of sed . | |
--help | Display a help message. | |
-n | Suppress automatic printing; sed will only print when explicitly told to. | |
-e script | Add the script to the commands to be executed. | |
-f file | Add the contents of file to the commands to be executed. | |
-i[SUFFIX] | Edit files in-place (makes backup if extension supplied). |
Creating an Example File with vim
Before diving into examples, let's create a sample file named example.txt
using vim
:
vim example.txt
Press i
to switch to insert mode and enter the following text:
This is a sed example.
sed is a stream editor.
Linux is fun.
Press Esc
, then type :wq
and press Enter
to save and exit vim
.
Examples of Using sed
Example 1: Simple Text Replacement
sed 's/sed/SED/' example.txt
Explanation:
- This command tells
sed
to search for the stringsed
and replace it withSED
in the fileexample.txt
. - The
s
in's/sed/SED/'
is the substitution command. - The text pattern to search for (
sed
) and the replacement text (SED
) are delimited by/
.
Example 2: In-place Editing
sed -i 's/Linux/UNIX/' example.txt
Explanation:
- The
-i
option makessed
edit the file in-place.sed
will directly modify the fileexample.txt
if this option is used. - This command replaces the first occurrence of
Linux
withUNIX
in each line.
Example 3: Global Replacement
sed 's/editor/EDITOR/g' example.txt
Explanation:
- The
g
at the end of the script tellssed
to perform the substitution globally on each line, not just the first occurrence.
Example 4: Print Only Matching Lines
sed -n '/sed/p' example.txt
Explanation:
- The
-n
option combined with thep
command tellssed
to only print the lines that match the pattern/sed/
.
Example 5: Delete Lines
sed '/fun/d' example.txt
Explanation:
- This command uses the
d
command to delete every line that matches the pattern/fun/
.
Example 6: Combining Multiple sed
Commands
sed -e 's/sed/SED/' -e '/Linux/d' example.txt
Explanation:
- The
-e
option allows you to specify multiple editing commands. This command will first replacesed
withSED
, then delete lines containingLinux
.
Example 7: Using a sed
Script File
First, create a sed
script file named script.sed
:
vim script.sed
Press i
and input:
s/editor/EDITOR/g
/Linux/d
Save and exit as before.
Now, run sed
with the script file:
sed -f script.sed example.txt
Explanation:
- The
-f
option tellssed
to read thesed
commands from the filescript.sed
. sed
will perform a global replacement ofeditor
toEDITOR
and delete lines containingLinux
.
Using sed
, one can perform sophisticated text processing tasks. The command's
syntax and options allow for extensive manipulation of text, from simple
replacements to complex transformations. It's an essential tool for anyone who
works with text files or streams in Linux.
What Can You Do Next 🙏😊
If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.