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
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
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
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…”.
We can now key in the line we wish to insert and save the file changes.
To save the file, key in the following while pressing [Enter] on your keyboard.
. w q
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
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
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
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.