31+ examples of Linux sed commands used in text operations

In the previous article, we talked about bash functions and how to use them directly from the command line, and we also saw some other cool stuff. Today we’re going to talk about a handy string manipulation tool called sed or sed Linux commands.

We use SEDs to process text files such as log files, configuration files, and other text files.

How to use Linux sed commands? In this post, we’ll focus on sed Linux commands, including related Linux sed command usage examples, which are used for text manipulation, which is an essential step in our bash scripting journey.

The Linux system provides some text processing tools; One of these tools is the Linux sed command text operation.

We will discuss 31+ examples of sed command usage with pictures to show the output of each example.

Learn about sed Linux commands

The sed command is a non-interactive text editor. The sed Linux command edits the data according to the rules you provide; You can use it like this:

$ sed options file

You’re not limited to using sed to manipulate files; You can apply it directly to STDIN like this:

$ echo "Welcome to LikeGeeks page" | sed 's/page/website/'

s command to replace the first text with the second text pattern. In this example, we replace the string “website” with the word “page”.

The example above is a simple example that demonstrates the tool. We can also use the sed Linux command to manipulate the file.

Let’s create an example file:

$ sed 's/test/another test/' ./myfile

The results are instantly printed on the screen; You don’t have to wait for the file to be processed to the end.

If your file size is large enough, you’ll see the results before the processing is complete.

The Sed Linux command doesn’t update your data. IT ONLY SENDS THE CHANGED TEXT TO STDOUT. The document remains unchanged. If you need to overwrite existing content, you can check out our previous post, which discussed redirects.

Use multiple sed Linux commands on the command line

How to use Linux sed commands? To run multiple sed commands, you can use the -e option, as follows:

$ sed -e 's/This/That/; s/test/another test/' ./myfile

You must separate the sed command with a semicolon without any spaces.

In addition, you can use single quotation marks to separate commands like this:

$ sed -e '

> s/This/That/

> s/test/another test/' myfile

Same result, no big deal.

Read the command from the file

You can save the sed commands in a file and use them by specifying the file with the -f option.

$ cat mycommands

s/This/That/

s/test/another test/
$ sed -f mycommands myfile

Linux sed command usage example: Replace flags

Take a closer look at the following examples:

$ cat myfile
$ sed 's/test/another test/' myfile

The above results show that we replace the first occurrence in each row. To replace all occurring patterns, use one of the substitution flags below.

We can write the logo like this:

s/pattern/replacement/flags

There are four types of substitutions:

  • g. Replace all occurrences.
  • A number that is the number of occurrences of the new text to be replaced.
  • p. Print the original content.
  • w file: indicates that the result is written to a file.

You can limit substitutions by specifying the number of occurrences that should be replaced, as follows:

$ sed 's/test/another test/2' myfile

As you can see, we have only replaced the second one in each row.

The g flag means global, which means all occurring global substitutions:

$ sed 's/test/another test/g' myfile

The p flag prints every line that contains a pattern match, and you can use the -n option to print only the modified line.

$ cat myfile
$ sed -n 's/test/another test/p' myfile

w flag to save the output to the specified file:

$ sed 's/test/another test/w output' myfile

We print the output on the screen, but we save the matching lines to the output file.

Linux sed command tutorial: Replace characters

Linux sed command text manipulation: Let’s say you want to search for the bash shell in the /etc/passwd file with sed and replace it with the csh shell, well, you can easily do it:

$ sed 's/\/bin\/bash/\/bin\/csh/' /etc/passwd

Oh!! That looks scary.

Fortunately, there is another way to achieve this. You can use an exclamation point (!) as a string separator, as follows:

$ sed 's!/bin/bash!/bin/csh!' /etc/passwd

It’s easier to read now.

How to use Linux sed commands? Limit SEDs

The sed command processes your entire file. However, you can restrict the sed command from processing specific lines; There are two ways to do this:

  • A series of lines.
  • Match the pattern of a specific row.

You can type a number to limit it to a specific line:

$ sed '2s/test/another test/' myfile

We’ve only modified the second line.

How about using a series of rows:

$ sed '2,3s/test/another test/' myfile

In addition, we can start with a line and end with the file:

$ sed '2,$s/test/another test/' myfile

Or you can use a pattern like this:

$ sed '/likegeeks/s/bash/csh/' /etc/passwd

Awesome!!

You can use regular expressions to write this pattern to make it more generic and useful.

Delete the row

Linux sed command text manipulation: To delete a line with sed, you can use the delete (d) flag to be your friend.

The delete flag deletes the text from the stream, not the original file.

$ sed '2d' myfile

Here we only remove the second line from the myfile.

How about deleting a series of rows?

$ sed '2,3d' myfile

Here we have removed a series of lines, the second and third lines.

Another type of range:

$ sed '3,$d' myfile

Here we remove from the third line to the end of the file.

All of these examples do not modify your original file.

$ sed '/test 1/d' myfile

Here we use a pattern to remove the rows that match the first row.

If you need to delete a series of lines, you can use the following two text modes:

$ sed '/second/,/fourth/d' myfile

We remove from the second line to the fourth line.

Linux sed command usage example: inserting and appending text

You can insert or append lines of text using the following flags:

  • (i) Marks.
  • (a) Signs.
$ echo "Another test" | sed 'i\First test '

We’ve added text before specifying the line.

$ echo "Another test" | sed 'a\First test '

We added the text after the specified line.

So, how about adding text in the middle?

Quite simply, look at the following example:

$ sed '2i\This is the inserted line.' myfile

Attachments work the same way, but look at the placement of the additional text:

$ sed '2a\This is the appended line.' myfile

We used the same flag, but with an insert or additional position.

Modify the rows

To modify a specific line, you can use the (c) flag, as follows:

$ sed '3c\This is a modified line.' myfile

You can use the regular expression pattern, and all lines that match that pattern will be modified.

$ sed '/This is/c Line updated.' myfile

Transform characters

The transform flag (y) works on characters like this:

$ sed 'y/123/567/' myfile

We apply transformations to all data, and we can’t limit them to specific events.

Print the line number

You can print line numbers using the (=) symbol, as follows:

$ sed '=' myfile

However, by using -n in conjunction with an equal sign, the sed command displays the line number that contains the match.

$ sed -n '/test/=' myfile

Linux sed command text operation: read data from a file

You can use the (r) flag to read data from a file.

You can define a line number or text pattern for the text you want to read.

$ cat newfile
$ sed '3r newfile' myfile

We inserted the content after the third line as expected.

Here’s how to use text mode:

$ sed '/test/r newfile' myfile

Cool, right?

How to use Linux sed commands? Useful examples

Let’s say we have a file that contains text with placeholders, and we have another file that contains data that will populate placeholders on another file.

We’ll use the (r) and (d) flags to do this.

Linux sed command tutorial: The word DATA in this file is a placeholder for the real content, which we will store in another file called data.

We’ll replace it with the actual content:

$ Sed '/DATA>/ {

r newfile

d}' myfile

Awesome!! As you can see, we populated the placeholder location with data from another file.

Linux sed command text manipulation: This is just a small introduction to sed commands. The sed Linux command is another world in itself.

The only limit is your imagination.

I hope you find it easy to manipulate strings with sed Linux commands, and I hope the above Linux sed command usage examples can help you, and if you have any questions, please comment below.

Thanks for reading.