SED

SED (short for stream editor) is a command-line utility that transforms text via a script. SED was based on the scripting features of the interactive editor ed. SED works as an in-place text editor. SED is a Stream Editor because it processes data line by line. SED is useful in pipelines to extract and clean text in a single pass, SED can also perform in place to files located directly on the disk.

How can you use SED?

The basic syntax for using SED in the shell is:

sed [OPTIONS] 'COMMAND' [INPUTFILE]

some of the available options in SED are:

Options

Description

-i

Edit the file in-place (overwrite)

-n

Prints only the changed lines.

-e

Allows multiple commands.

-f

Reads SED commands from a file.

-r

Use extended regular expressions.

What does the command look like?

sed '[i,j] s/To_Be_Replaced/New_text/[k]/[g]/[p]' example.txt

[i,j] are optional arguments saying the line range you want your command to be run through.

[k] is the optional argument saying you want to replace the kth occcurence of your targeted text.

[g] allows you to replace more than one occurrence of your targeted text per line.

[p] Prints the changed line twice.

Using SED?

Lets say we are working with this example input below:

Debian is a unix based operating system developed on the unix kernel.
Ubuntu is a unix based operating system developed on the unix kernel.
MacOs is a unix based operating system developed on the unix kernel.
Unix is a unix based operating system.
  1. Replacing a single instance per line.

Input Command: sed '1,3 s/unix/linux/' example.txt

Debian is a linux based operating system developed on the unix kernel.
Ubuntu is a linux based operating system developed on the unix kernel.
MacOs is a linux based operating system developed on the unix kernel.
Unix is a unix based operating system.
  1. Replacing every instance per line.

Input Command: sed '1,3 s/unix/linux/g' example.txt

Debian is a linux based operating system developed on the linux kernel.
Ubuntu is a linux based operating system developed on the linux kernel.
MacOs is a linux based operating system developed on the linux kernel.
Unix is a unix based operating system.
  1. Replacing a single line and viewing the only changed line.

Input Command: sed -n '4 s/unix based/foundational/p' example.txt

Unix is a foundational operating system.
  1. Making our statements accurate.

To make our statements accurate, we need to use a combination of selective line occurrence and word changes, along with -e, to run multiple SED commands in a single call.

Input Command: sed -e '4 s/unix based/foundational/' -e '1,2 s/unix/linux/2g' -e '3 s/unix/XNU/2g' -e 's/based/like/' example.txt

Debian is a unix like operating system developed on the linux kernel.
Ubuntu is a unix like operating system developed on the linux kernel.
MacOs is a unix like operating system developed on the XNU kernel.
Unix is a foundational operating system.
  1. Advanced Usage: Updating syntax of deprecated command.

To properly understand what this command is doing, we want to break down what each symbol is interpreted as. First, we must find our intended target. In this scenario, we are updating Python scripts from Python 2 to Python 3, so before we begin substitution s/ we want to find the word print so /print/s/.

"[^"]*" is used to denote a string of characters in between double quotes, the * or glob symbol is used to expresses a group of chracters, [^"] is used to denote a character that is not a double quote. Essentialy "[^"]*" is asking for a double quote followed by any amount of characters until reaching another double quote. When it comes to the replacement, we want to enclose our double quotes in parentheses,but how are we going to copy that text? & ampersand is used as a reference to our substituted section, so (&) will enclose our double quote marked sections with parentheses.

-e is used to run more than one SED command with a single call from the command line. This allows us to find where our double quote sections live and then clean up the remaining whitespace left behind by our command.

Input Text: print "Hello World"

sed -e '/print/s/"[^"]*"/(&)/g' -e 's/ (/(/' print_example.py

print("Hello World")

One of the final steps when using a SED program is calling the optional argument -i to overwrite your text file; this should only be done when you know your SED command will have the expected output.

Additional Resources.

SED is a powerful utility for cleaning up your text-based documents. Using SED effectively involves understanding regular expressions in Unix. To better understand the SED command itself, feel free to consult the SED man pages.