Insert a Line at Specific Line Number


Introduction

Inserting a line at a specific line number in a file can be a useful task when you want to add new information to an existing file, or when you want to reorganize the contents of the file. In Linux, there are several ways to achieve this, but in this article, we will focus on using the sed command to insert a line at a specific line number.

What is the sed command?

sed stands for "stream editor" and it is a command line utility in Linux that allows you to modify the contents of a file or stream by applying various operations to it. Some of the operations that sed can perform include replacing text, deleting lines, and inserting lines. sed is often used for automated text processing and for making bulk changes to a file.

Insert a Line at a Specific Line Number Using sed

To insert a line at a specific line number using sed, you can use the i command followed by the line you want to insert. The i command tells sed to insert the specified line before the line number specified.

For example, to insert the line "This is a new line" at line number 3 in a file called file.txt, you can use the following command

$ sed '3i This is a new line' file.txt

This will insert the line "This is a new line" at line number 3 in the file file.txt. If you want to see the output of the sed command, you can use the -n option to suppress the default output and only print the modified lines. For example

$ sed -n '3i This is a new line' file.txt

This will output only the modified lines, which in this case would be the line "This is a new line" inserted at line number 3.

Modifying the Original File

By default, the sed command does not modify the original file. Instead, it outputs the modified version of the file to the standard output (i.e., the terminal). If you want to save Insert a Line at Specific Line Number the changes made by the sed command to the original file, you can use the -i option to edit the file in place. For example

$ sed -i '3i This is a new line' file.txt

This will insert the line "This is a new line" at line number 3 in the file file.txt, and the changes will be saved to the original file.

Examples

Here are some additional examples of using the sed command to insert a line at a specific line number

  • To insert the line "This is another new line" at the beginning of the file file.txt, you can use the following command

$ sed '1i This is another new line' file.txt
  • To insert the line "This is yet another new line" at the end of the file file.txt, you can use the following command

$ sed '$a This is yet another new line' file.txt

The “$” symbol represents the last line of the file, and the “a” command tells sed to append the specified line after the line number specified.

Conclusion

In this article, we explained how to insert a line at a specific line number on Linux using the sed command. The sed command is a powerful tool for modifying the contents of a file, and the i command allows you to easily insert a new line at any line number. By using the -n and -i options, you can control the output and save the changes to the original file.

It is important to note that the sed command operates on a single line at a time, so it may not be suitable for inserting multiple lines or for inserting lines in the middle of a file. In such cases, you may want to consider using other tools such as awk or perl.

In conclusion, the sed command is a useful tool for inserting a line at a specific line number on Linux. It allows you to easily modify the contents of a file and save the changes to the original file. By following the steps outlined in this article, you can insert a line at any line number using the sed command.

Updated on: 04-Jan-2023

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements