“In UNIX/Linux ecosystem, the sed command is a dedicated tool for editing streams, hence the name (stream editor). It receives text inputs as “streams” and performs the specified operations on the stream.”
In this guide, we will explore performing in-place file editing with sed.
To perform the steps demonstrated in this guide, you’ll need the following components:
Editing Stream Using sed
First, let’s have a brief look at how sed operates. The command structure of sed is as follows:
The following command showcases a simple workflow of sed:
- The echo command prints the string on STDOUT. Learn more about STDIN, STDOUT, and STDERR.
- We’re piping the output to the sed Here, STDOUT is the stream sed that will perform the task specified.
- The sed command, as specified, will search for any instance of the word quick and replace it with fast. The resultant stream will be printed on the console.
What if we wanted to modify the texts of a text file? The sed command can also work using text files as the stream. For demonstration, I’ve grabbed the following text file:
The following sed command will replace all the instances of the with da:
Check the content of demo.txt for changes:
From the last example, we can see that sed only printed the resultant stream on the console. The source file (demo.txt) wasn’t touched.
Editing Files In-place Using sed
As demonstrated from the previous example, the default action of sed is to print the changed content on the screen. It’s a great feature that can prevent accidental changes to files. However, if we wanted to save the changes to the file, we needed to provide some additional options.
A simple and common technique would be replacing the content of the file with the sed output. Have a look at the following command:
Here, we’re overwriting the contents of demo.txt with the output from the sed command.
While the command functions as intended, it requires typing additional codes. We involved the cat and tee commands along with the sed command. Bash is also involved in redirecting the outputs. Thus, the command is more resource-intensive than it needs to be.
To solve this, we can use the in-place edit feature of sed. In this mode, sed will change the contents of the file directly. To invoke the in-place edit mode, we have to use the -i or –in-place flag. The following sed command implements it:
Check demo.txt for changes:
As you can see, the file contents are changed without adding any additional components.
In this guide, we successfully demonstrated performing in-place edits on text files using sed. While sed itself is a simple program, the main source of power lies within its ability to incorporate regular expressions. Regex allows describing very complex patterns that sed acts upon. Check out regex in sed to learn more in-depth.
Alternatively, you can use Bash scripts to filter and modify the contents of a file. In fact, you can incorporate sed in your scripts to fine-tune text content. Check out this guide on getting started with Bash scripting.