- Docker logs tail: Troubleshoot Docker containers with real-time logging - Wed, Sep 13 2023
- dsregcmd: Troubleshoot and manage Azure Active Directory (Microsoft Entra ID) joined devices - Thu, Aug 31 2023
- Ten sed command examples - Wed, Aug 23 2023
The Linux sed command is short for "stream editor" and is commonly used to change or substitute characters or text in text, config, and other files. For instance, what if you want to search for a specific line of text in a config file and replace it with something else? The sed command can easily do this for you, as we will see in our examples below.
Let's take each of these examples.
Find and replace matched text
Our first example has us using sed to find and replace text. Before we run the command, notice the contents of the test file, file.txt, that we created. We populated it with the word "find".
Now let's run the find and replace. Using the -i parameter, we will edit the file in place. If the -i parameter weren't used, sed would just display the changes it would make at the terminal prompt without actually making the changes.
sed -i 's/find/replace/g' file.txt
In the context of the sed command for find and replace, the s and g flags have specific meanings, and the slashes / are used as delimiters. Here's a detailed explanation:
- s: This is the substitute command. It tells sed that you want to perform a substitution operation. It's the first character in the expression and is always followed by a delimiter (commonly a slash /).
- /: These delimiters separate the different parts of the substitution command. In the expression 's/find/replace/g', the slashes separate the s command, the pattern to find (find), and the replacement text (replace).
- g: This is the global flag. When included at the end of the expression, it tells sed to replace all occurrences of the pattern in the line. Without the g flag, only the first occurrence of the pattern in each line would be replaced.
Now, if we check the text file, here is what we see. The sed command found every occurrence of the word "find" and replaced it with the word "replace".
Replace text regardless of case
Another handy feature of sed is that we can replace text by disregarding case. For example, we may have a file that looks like this:
Without ignoring the case, the two occurrences of "Find" with the uppercase F will not be replaced. To ignore the case of the words you want to find, add an "i" to the end of the "g" in the sed string. Use the following sed command:
sed -i 's/find/replace/gi' file.txt
Replace based on specific lines
With sed, you can also replace only text found on specific lines. For instance, you may want to find and replace text found between lines 5 and 10:
To do this with sed, you can use the following:
sed -i '5,10s/find/replace/g' file.txt
Running the command results in the following changes to the text file:
Delete lines of text based on position
Next, let's use sed to delete lines of text. This is a helpful capability of sed since we can use it to direct sed to delete a specific line in a file. Note the following text file, where we have labeled each line of text.
Now, let's direct the sed command to delete a specific line of text:
sed -i '3d' file.txt
This command tells sed that we want to delete the third line down from the top of the file.
When the command is run, sed deletes the "3rd line" of text from the top of the file.
Delete text based on a pattern
We can also delete text based on a pattern instead of a position. For example, what if we want to delete a line that contains the text pattern "8th", but we don't know whether the text is the 8th line in the text file?
To do this, we can use the following command:
sed -i '/8th/d' file.txt
In this example, the "d" flag tells sed to delete every line that contains "8th".
As you can see in the screenshot below, the line containing "8th line" has been removed.
Insert text
Now, let's consider an example where we want to insert text in a file. Let's see if we can insert the text that we deleted in the previous example. We can do this with the following code, which tells sed to insert "3rd line" in the third line of text with the "3i" designation:
sed -i '3i\3rd line' file.txt
Again, we are using the -i parameter to edit the file in place. After running the command, we see that the "3rd line" text has been added back to the file.txt file.
Append text
Finally, let's look at how to use sed to append text to our file.txt file. The following command will append the text after the 4th line. The text that will be appended is "Let's append text".
sed '4a\Let's append text' file.txt
After running the command, let's check our file.txt file.
Write to a new file
So far, we have been making changes to the file on the fly. What if you want to create a new file with the changes instead of manipulating the original file? We can do this using the following syntax:
sed 's/find/replace/g' original_file.txt > new_file.txt
- s: This is the substitute command. It tells sed that you want to perform a substitution operation.
- /: These delimiters separate the different parts of the substitution command. In the expression 's/find/replace/g', the slashes separate the s command, the pattern to find (find), and the replacement text (replace).
- g: This is the global flag. When included at the end of the expression, it tells sed to replace all occurrences of the pattern in the line. Without the g flag, only the first occurrence of the pattern in each line would be replaced.
- original_file.txt: This is the original file we want to modify.
- ">": We are telling the sed command to create a new file.
- "new_file.txt": The name of the new file.
Print a specific line
Sed also provides the ability to print a specific line of text on the screen. Use the following command to print the third line of text:
sed -n '3p' file.txt
Here, the -n option suppresses automatic printing, and the p command prints the specified line on the screen.
Subscribe to 4sysops newsletter!
Wrapping up
The sed command is a powerful tool for manipulating files through Bash scripting, making it unnecessary to manually locate text strings and alter them. This post provides several sed command examples to get you started with string manipulation under Linux. You can find more information in the command reference.
Good information. I never could get it to work properly.
Nice summary. I was glad to see the use of “gi” suffix to replace all occurrences. It was missing from table of contents as a specific example, and it is #1 use of sed for me when doing data cleansing activities. Suggest showing example on how to replace value in json file, and adjust configuration i yaml ( such as value for hosts or gather_facts in ansible playbook.