1. Overview

The sed command is a powerful weapon we can wield to process text under the Linux command line. For example, using the sed command, we can remove a line from a file, substitute a line, or insert a new line in a file.

In this tutorial, we’ll take a closer look at how to use the sed command’s feature for inserting a new line when the new line contains spaces.

2. Introduction to the Problem

First of all, let’s take a look at an input file:

$ cat input.txt 
A new line will be inserted below me:
---------------------------
This is the end of the file.

The file input.txt looks pretty simple. It contains only three lines. Now, what we would like to do is to insert a new line after the first line. Of course, the new line will contain space characters.

If we think about the problem carefully, there can be three scenarios:

  • First, the spaces are in the middle of the line.
  • Second, the line has trailing spaces.
  • Third, the line contains leading spaces.

There are different ways to insert a new line in a file using sed, such as using the “a” command, the “i” command, or the substitution command, “s“.

sed‘s “a” command and “i” command are pretty similar. The only difference is that i” inserts the new line before the address, while “a” will append the new line after the address.

In this tutorial, we’ll be focusing on sed‘s “a” command.

Next, we’ll go through the three scenarios and address how to insert a line with spaces using sed.

3. Inserting a Line With Spaces in the Middle

Now, let’s insert a new line with spaces as the second line in the file using sed‘s “a” command:

$ sed '1 aI am the new line.' input.txt 
A new line will be inserted below me:
I am the new line.
---------------------------
This is the end of the file.

As the output above shows, the command works straightforwardly.

The “1” in the command is an address, which indicates the first line.

Therefore, we can translate the command sed ‘1 a…’ into “when the line number is 1, then append a new line after it”.

Next, let’s find out if the command still works when the line to insert has trailing spaces.

4. Inserting a Line With Trailing Spaces

Now, let’s add some trailing space and give the same command a try. We’ll pipe the output of the sed command to the cat command with the -e option so that we can verify trailing spaces easily:

$ sed '1 aI am the new line with trailing spaces   ' input.txt | cat -e
A new line will be inserted below me:$
I am the new line with trailing spaces   $
---------------------------$
This is the end of the file.$

This time, as the output above shows, the same command works in this scenario, too.

5. Inserting a Line With Leading Spaces

First, let’s try the same command once again and hope it works for the case of the leading spaces:

$ sed '1 a  I am the new line with leading and trailing spaces   ' input.txt | cat -e
A new line will be inserted below me:$
I am the new line with leading and trailing spaces   $
---------------------------$
This is the end of the file.$

As we can see, the leading spaces have disappeared in the output. So, the command didn’t work as we expected.

To solve the problem, we need to add a backslash “\*” after the “a*” command:

$ sed '1 a\  I am the new line with leading and trailing spaces   ' input.txt | cat -e
A new line will be inserted below me:$
  I am the new line with leading and trailing spaces   $
---------------------------$
This is the end of the file.$

6. Understanding the Backslash After the sed “a” and “i” Commands

We’ve learned that sed‘s “a” and “i” commands can insert or append a new line.

The backslash character after the “a” or “i” command doesn’t function as the part of an escape sequence, such as \t as a tab or \n as a newline. Instead, it indicates the beginning of the text in the new line we’re inserting.

Even if the backslash and the first character of the new line together may match an escape sequence, sed will not interpret the combination as an escape sequence.

Probably, an example can explain it quickly:

$ sed '1 a\n (1) and \n (2)' input.txt
A new line will be inserted below me:
n (1) and 
 (2)
---------------------------
This is the end Of the file.

In this test, we have two occurrences of “\n“. The second one has been interpreted as a newline character because it’s a part of the content.

However, sed interpreted the first “\n” as a regular “n” character, as the backslash just marked the beginning of the new content.

*When we use sed‘s “a” or “i” commands, it’s a good practice to precede the new content with a backslash.*

7. Conclusion

In this tutorial, we’ve addressed how to insert a new line that contains spaces using the sed command through examples.

Moreover, we’ve discussed the function of the backslash after sed‘s “a” and “i” commands.