How to Use sed to Replace String in a File

Dealing with files is a core part of being a programmer. Over time, you may want to make edits to the Strings that lie within these files. Opening up the string file each time can be tedious to find and change the required Substring/String. For this purpose, a command “sed” exists, which assists in finding and editing any script from a file. This command is executed in the Linux terminal or bash scripts to allow the changes to be made to the required file.

This article will provide a detailed guide on how this command can be utilized to replace strings in a file.

How to Use “sed” to Replace String in a File?

There are a few different methods in which the “sed” command can be utilized using the terminal to edit strings inside a file. Let’s take a sample text file, “sample.txt” for our example. The content of the “sample.txt” is provided below:

Let’s check out how the “sed” command can be applied to this text file.

Example 1: Replace a Case-Sensitive String

Firstly, let’s have a look at the most standard “sed” command that can be used in the Linux terminal. The format for the “sed” command is shown below:

$ sed -i 's/search/replace/' sample.txt

The command above takes a “search” string and checks for it inside the “sample.txt”. Once the String is discovered, it is swapped with the “replace” String. For this scenario, it is important to ensure that the search string we entered is case-sensitive. Let’s take a sample command for this:

$ sed -i 's/harris/luke/' sample.txt

This command takes the string “harris” and replaces it with the string “luke”. This is demonstrated below:

Example 2: Replace a Case-Insensitive String

The “sed” command provides additional properties which will allow the command to search the file for the required string in a case-insensitive manner. The sample command to achieve this would be as follows:

$ sed -i 's/search/replace/i' sample.txt

The “i” option at the end ensures that the search string searches the file in a case-insensitive manner. This is demonstrated with the command below:

In this command, the search string contains a capital “H”(“Harris”), whereas the file contains a small “h”(“harris”). Regardless of this difference, the replacement is completed due to the “i”(case-insensitive) command at the end. This replacement is demonstrated in the file below:

Example 3: Search and Replace Patterns

The “sed” can be used not only to search for Strings but also to find patterns using regular expressions. In this example, we wish to insert the two-digit age present in the file in square brackets. The following command exercises a regular expression to find and replace the required string:

$ sed -i 's/\b[0-9]\{2\}\b/[&]/g' sample.txt

The “[0-9]” specifies the digits that need to be checked and the 2 inside the “{ }” brackets tells that the number should be two digits. The & sign acts as the symbol for the output.

The above example shows how any pattern can be matched. In this command above, a number with two digits is searched and surrounded by square brackets using the “&” command. This option acts as the string that fits the pattern described before it.

Conclusion

The sed command can search case-sensitive and case-insensitive strings and replace them with the desired string or pattern. For a case-insensitive search, the “i” option is added at the end of the command before the file name. The sed command can also be used to search patterns such as numbers with a certain number of digits and then perform a replacement on the Strings that match these patterns. This article has described various functionalities of the “sed” command to search and change strings inside a file.