How to use the command SED to find and replace
The command “sed” is a powerful Unix utility and stream editor that enables users to parse and transform text. Despite being available since 1974, oftentimes people remain unaware of how to best utilize its powers in order to streamline tasks or challenges they are working on.
Sed works by reading files, line by line, into an internal buffer also known as the pattern space. Within this pattern space, sed applies a command that is given to it via the sed script provided by the user. This script entails one or more operations that encapsulate what the user desires to happen within the file. After sed is finished reading all lines of the file and applying modifications, it closes and the original file now contains the desired transformations
Mastery of sed particularly has a lot of possible implications for the automation of tasks, mass file processing, or when working with templates, as you will see below. Essentially, what sed allows is the finding of a special string in a file and replacement with another string, all without opening the file itself. You can see why this makes it a perfect command for automation! With this one simple example, we show you exactly how you can start using sed for yourself today:
First, we need to create a text file with some standard content to later modify with sed. In your chosen directory, run the command below:
echo Good morning, what a nice morning today. > file.txt
Good morning, what a nice morning today.
Now that we have our text file, we can change the word “morning” to “afternoon” using sed on the command line. There are several modifications you can make when running sed that allow you to specify exactly how you want to replace your chosen expression. In our command below, we use the -i option to edit our file in place, so without opening it, and the -e option to indicate that this is the sed script we want to run on our file. The expression itself contains s/ to represent which words are being replaced by what, and /g to indicate replacing our expression globally, so not only the first instance of “morning” will be modified in case it shows up more than once on the same line.
sed -i -e 's/morning/afternoon/g' file.txt
Good afternoon, what a nice afternoon today.
This changes the file. We can now double check the new content of the updated text file:
You will see how “morning” has now been changed to “afternoon.” That’s it, all you need to start using sed immediately!
You are now ready to use sed to replace file content on the fly. sed is a powerful utility with a huge number of possible applications and there are far many more existing options you can use with sed in order to really feel its power. Even by using sed to replace particular expressions or patterns within files, without opening the files themselves, as shown in this tutorial, you can save yourself a lot of time and effort in an endless variety of tasks. If this guide was helpful to you, kindly share it with others who may also be interested.