Learn how to manipulate text in Linux using the powerful sed command. This post covers sed command examples of finding and replacing text based on text matching, case-insensitive operations and position, deleting lines based on position and pattern, appending text, writing to a new file, and printing specific lines.

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".

Test file for sed find and replace

Test file for sed find and replace

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:

  1. 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 /).
  2. /: 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).
  3. 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.
Running the find and replace using sed

Running the find and replace using sed

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".

Using sed we ran the find and replace command and replaced a specific word

Using sed we ran the find and replace command and replaced a specific word

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:

Text file with uppercase and lowercase

Text file with uppercase and lowercase

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
Running sed as case insensitive

Running sed as case insensitive

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:

Replacing text on specific lines

Replacing text on specific lines

To do this with sed, you can use the following:

sed -i '5,10s/find/replace/g' file.txt
Running the find and replace using sed 1

Running the find and replace using sed 1

Running the command results in the following changes to the text file:

Text is replaced between specific lines

Text is replaced between specific lines

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.

Test file to delete a specific line

Test file to delete a specific line

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.

Running sed to delete a specific line of text

Running sed to delete a specific line of text

When the command is run, sed deletes the "3rd line" of text from the top of the file.

The third line has been deleted

The third line has been deleted

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".

Deleting based on pattern

Deleting based on pattern

As you can see in the screenshot below, the line containing "8th line" has been removed.

Line is deleted based on pattern matching

Line is deleted based on pattern matching

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.

The sed command has properly inserted the text

The sed command has properly inserted the text

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.

Text appended after the specified line using sed

Text appended after the specified line using sed

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
  1. s: This is the substitute command. It tells sed that you want to perform a substitution operation.
  2. /: 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).
  3. 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.
  4. original_file.txt: This is the original file we want to modify.
  5. ">": We are telling the sed command to create a new file.
  6. "new_file.txt": The name of the new file.
Creating a new file with changes instead of changing the original

Creating a new file with changes instead of changing the original

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!

Printing a specific line of text

Printing a specific line of text

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.

avataravatar
2 Comments
  1. Allan 1 month ago

    Good information. I never could get it to work properly.

  2. Tim 1 month ago

    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.

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account