sed command in linux 1

How to Use sed Command in Linux

When it comes to Linux word-processing, the sed command in Linux is a popular option due to its convenience and versatility. You can use it to create, edit, and delete files in just a few keystrokes. But there is more to it than its basic options once you get familiarized with its secondary commands.

What is sed in Linux?

Stream Editor or sed command in Linux is a word-processing utility that allows users to process text files and scripts within the command line. You can use this tool to modify scripts or edit text files in general. The learning curve is very steep for this command, but it is worth it once you get the hang of it.

sed is often used for substituting content on text files without manually opening and editing them. Hence, the editing process can be automated in a single command or via scripts. It can process data from commands, files, and standard input.

sed Command in Linux

System administrators also use this command because it is more efficient and is considered safer than visual editors like vim. All of the results are displayed in the standard output and are not applied directly to the file unless specified.

Elaborating all the possible sed uses and syntaxes themselves will need a book to map out. However, its simplest usages are easy to understand.

It can display, find, substitute, add, and delete content for thousands of files in just one line of instruction. Building a strong fundamental in its basic functions will make you a reliable sed user.

sed vs Other Editors

Other text editors such as vim can also be utilized to edit files. However, what makes sed favorable to a lot of users is its potential for automation. Functionally, it creates a new “pattern space” to apply the edits so you can test and preview the changes before making a permanent change.

Because of its fundamental workings, you can check and modify your instructions before actually executing them on the actual file. Creating a streamlined sed instruction is often easier and than manually making the changes yourself with a minimal margin of error.

Default sed Syntax

In its common usages, a sed instruction is usually comprised of four parts: the command, the option, the arguments, and the target file. Most of its versatility occurs in the third part (arguments), wherein you can add secondary commands, delimiters, and regular expressions.

$ sed [option] ‘arguments’ filetarget

All sed results are shown in the standard display. You can “pipe out” the output to a separate file or you can officially write the changes directly with the -i option. But by default, the changes are not made in the file unless specified.

Relevant Options for sed Command in Linux

This list shows all the commonly-used command-line options for sed. Users can omit this part completely if they don’t need the applied effect.

-i: Directly edit the file

-n: Only prints affected content

-f:  Used for file-based sed instructions

-e: Used for command-line instructions

Regular Expressions

sed is a very versatile tool because it can do additional processing with regular expressions or “regex”. These expressions are used alongside secondary commands: p (for printing lines), d (for deleting lines), a (for appending changes), and s (for substituting content).

^: First instance only (line only)

$: Last instance only (line only)

*: Matches zero or more occurrences of the previous character

+: Matches one or more occurrences of the previous character

?: Matches zero or one occurrence of the previous character

.: Matches one character

[0-9]: Indicates every numeric character

[a-z]: Indicates all lowercase letters

[A-Z]: Indicates all capital letters

&: Match strings completely

The Substitution Command Format

Substitute s command is often where a lot of complicated prompts appear. When you combine commands, delimiters, flags, and all regex in one line, the prompt can get very confusing. In general, the substitution format is executed with the form below.

$ sed ‘s/pattern/action/flag’ filetarget

The “pattern” part is the set of conditions that sed follows to find certain words or characters. “Action” is the set of instructions executed to the search results afterward.

Pattern Flags

You need to specify if you want to change all instances that match the predefined conditions or only some parts of it. To do this, you need to modify the pattern flag.

g: replace all matches

nth: replace only the specified instance (numbers only)

Combining Things for Substitution

Combining all the things mentioned above, here’s how a typical substitution instruction looks like below. Breaking down the code, the ^ indicates that sed would replace all the first letters with “N” on every line as long as it starts with the letter “M”.

$ sed ‘s/^M/N/g’ moon.txt

The global flag g states to do this with all the instances throughout the document.  However, only the first “Moon” word was changed because sed executes processes per line, and there is only one line in the example.

using subsitute with sed command in linux

Example Uses of sed Command in Linux

Sed has vast utility and usages, but knowing the basics would be enough to build your knowledge and skills about the command. Here are some of the basic uses of sed to show or edit simple text files.

1. Display Specific Line of Texts

In this example the sed option -n indicates to only show the lines between 1,3p lines. It also allows sed to hide unneeded lines. Displaying specific lines can be very useful if the user just wants a quick look at the file without cluttering the Terminal. 

sed command in linux

2. Find and Replace Texts

It can be noticed that the example below uses -i, which means that all of the changes are applied directly into the file. The substitution command was configured to find all instances of “Iskall” and replace it with “Keralis.”

sed command in Linux finding and replacing texts

3. Adding Content

Adding content is only available on a “per line” basis due to how sed works unless you use the substitute command. In this example, the “4” indicates where the appended content would be inserted (after line 4). a lets sed append string following the command.

adding content with sed command

4. Deleting Content

Below is one of the easiest ways to remove content using the sed command in Linux. Using the substitution command, all instances of Iskall were replaced by empty characters. If you want to delete an entire file, you can also do sed ‘nd’ file target where nd is the line number.

deleting content with sed

5. Replacing Text After nth Occurrence

To replace a specific number of matches, the easiest way is to change the pattern flag. Sed will only change the nth instances of the matched word per line. In this example, only the third instance of “Big” was replaced with “Crashed” based on the specified number in the pattern flag.

replacing text with sed

6. Executing Multiple sed Commands

You can also execute multiple instances of sed in one line. However, all of these are executed separately. If you want to make changes, you need to use -i.

multiple sed instances

And that’s it for the sed command in Linux. sed is a very reliable command, but it doesn’t really have good documentation due to its limitless potential. But hopefully, this guide was able to give you the fundamental concept of the command.

If this guide helped you, please share it. ?

Leave a Reply
Related Posts