Sed command in Linux

Mustafa AK
2 min readJan 13, 2023

--

The sed command in Linux is a powerful tool for text manipulation. It can be used to perform basic text transformations on an input stream (a file or input from a pipeline). In this article, we'll explore some advanced usage examples of sed that can be particularly useful for text processing and automation tasks.

First, let’s review some basic usage of sed. The basic syntax is:

sed 'command' file

Where command is a sed command and file is the file you want to manipulate.

A common use of sed is to replace all occurrences of a word or phrase in a file with another word or phrase. The following command will replace all occurrences of the word "old" with the word "new" in the file "file.txt":

sed 's/old/new/g' file.txt

Now, let’s move on to some advanced usage examples.

Editing a file in-place

By default, sed will output the modified text to the console. If you want to edit a file in-place, you can use the -i option. For example, the following command will replace all occurrences of the word "old" with the word "new" in the file "file.txt" and save the changes to the file:

sed -i 's/old/new/g' file.txt

Using regular expressions

sed supports regular expressions, which allows for more powerful text manipulation. For example, the following command will delete all lines in a file that contain the word "delete":

sed '/delete/d' file.txt

Using back-references

In sed, you can use back-references to refer to the matched text in the replacement string. Back-references are specified with the \n syntax, where n is the number of the capture group. For example, the following command will change the word "apple" to "orange" only if it is preceded by the word "fruit":

sed 's/fruit\(apple\)/fruit\(orange\)/g' file.txt

Using sed in a script

sed is often used in scripts to perform automated text processing tasks. Here's an example of a simple script that uses sed to update all URLs in an HTML file to use HTTPS instead of HTTP:

#!/bin/bash
sed 's/http:\/\//https:\/\//g' index.html > index_https.html

Conclusion

sed is a powerful tool for text manipulation in Linux. The examples in this article should give you a good starting point for using sed to automate text processing tasks. Keep in mind that sed is just one of many text processing utilities available in Linux, and it's always worth exploring other options to find the best tool for the job.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Mustafa AK
Mustafa AK

Written by Mustafa AK

DevOps Engineer @ relayr | 2 x AWS and 2 x Azure Certified Cloud Engineer | Terraform | CKA | CKAD

Responses (1)

Write a response