How to Insert a Line at a Specific Line Number

Supposing you have a large file that needs some editing and you are looking for the quickest way of achieving such an objective via a Linux operating system environment, what do you do? If you already have access to your target file and can identify each line number associated with the file content, then your problem is half solved.

This article will walk us through various approaches to inserting a line at a specific line number on an editable file under a Linux operating system.

Problem Statement

Supposing we have a file called sample_file.txt that needs an extra line inserted at a specific line number.

$ cat sample_file.txt
View File Contents in Linux
View File Contents in Linux

Let us assume the above file opened by the cat command is extremely large. The first step before looking at viable solutions for inserting a line in the above file is to number it.

Since we have assumed we are dealing with a large file, we can use the less command plus the command option -N to number it.

$ less -N sample_file.txt
Show Line Number When Viewing File
Show Line Number When Viewing File

1. Using ed Command

As per its manual page, ed is a line-oriented text editor with the capability of not only creating, displaying, and modifying, but also manipulating text files.

If we combine the ed command with our file, it will output the number of characters present in it.

$ ed sample_file.txt
Count Number of Characters in File
Count Number of Characters in File

Since ed is an interactive editor, the execution of the above command does not return to prompt.

Supposing we want to insert a line in our file after line number 7 (fffff), the associated ed command will look like the following:

7i

This command can be translated as “in line 7, insert…”.

Insert Line in File
Insert Line in File

We can now key in the line we wish to insert and save the file changes.

Insert Text in File
Insert Text in File

To save the file, key in the following while pressing [Enter] on your keyboard.

.
w
q
Save File in Linux
Save File in Linux

The command has updated the total number of characters from 264 to 288 and you can confirm it by viewing the file contents.

$ cat -n sample_file.txt
Insert Text to Specific Line in File
Insert Text to Specific Line in File

As you can see, the line 7 entry is what we keyed in using the ed command.

2. Using sed Command

If you need to filter and transform your texts via the Linux command-line environment, sed is one of the commands you need to consider. This utility is associated with many file operations. We could say that it was derived from ed but without interactive functionality.

To insert a line of line number 10, the sed command to use will look like the following.

$ sed -i '10 i sed command put me here!' sample_file.txt

The -i command option initiates the insertion of the stringed line in line number 10 of the sample_file.txt file.

Next, confirm the line 10 entry with the cat command:

$ cat -n sample_file.txt
Insert Text to Particular Line in File
Insert Text to Particular Line in File

3. Using awk Command

AWK programming language is useful in text retrieval and processing and also in data file manipulation. It is non-interactive and reads file inputs line-by-line.

We can use it to add an extra line to our file but we’ll have to redirect the output to a new file to save it since AWK does not have the capabilities of saving file changes on the original input file.

$ awk 'NR==5{print "awk command put me here!"}1' sample_file.txt > final_file.txt 

NR==5 checks for the condition if the Number of Records (NR 5 or line 5) exists, and then proceeds to execute (1) the print statement inside the curly braces.

Confirm line 5 entry with the cat command:

$ cat -n final_file.txt
Add Text to Particular Line in File
Add Text to Particular Line in File

Do check the video version of this article is available here.

Know of other unique ways of inserting a line at a specific line number under a Linux OS environment? Feel free to leave a comment.

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.