insert text certain line in linux

How to Insert Text At Certain Line in Linux

Linux provides many tools to help you work with text files. It is easy to add new text at the beginning or end of a file in Linux. But sometimes you may need to insert text at certain specific line in Linux. In this article, we will learn how to insert text at certain line in Linux.


How to Insert Text At Certain Line in Linux

There are several tools to insert text at certain line in Linux. We will see how to do this using ed, sed, and awk commands.


1. Using ed

Ed is an interactive line editor. Let’s say you have file data.txt.

$ cat data.txt
hello
good
morning

When you enter your filename after ed command, it displays the total number of lines in it.

$ ed data.txt
4

After that it will wait for your input. In order to insert text at line 3, enter 3i followed by the new text. You can add one of more lines.

3i
this is new text

Once you are done, enter dot, then w and then q, to save changes and quit. Remember, you won’t see the prompt when you enter these keys since you are inside the ed editor.

.
w
q

Now if you view the file, you will see the new line is inserted.

$ cat data.txt
hello
good
this is new text
morning


2. Using sed

sed command is a powerful text editor, based on ed utility. However, it is not interactive like ed. In sed, you have to specify the modification along with the file name.

$ sed '3 i this is new text' data.txt
hello
good
this is new text
morning 

Basically, sed does everything that we did with ed, in one line. It is much shorter and more powerful. In the above command, just like ed, 3 means line number 3 and i means insert.

If you don’t want to change the input file, you can always redirect the output to new file.

$ sed '3 i this is new text' data.txt > new_data.txt
$ cat new_data.txt
hello
good
this is new text
morning 

That is why sed is much better than ed and more preferred.


3. Using awk

awk is another non-interactive tool like sed that allows you to read one or more lines from a file. But it always reads the file line by line. But it also provides programming constructs like C language to perform complex tasks with files. Here is the awk command to insert text at line 3.

$ awk 'NR==3{print "this is new line"}1' data.txt

We need to specify condition{action} pair within single quotes. NR==3 means line number 3. Since awk reads the file line by line, when it reaches line 3, it will execute the command inside {}, that is, print ‘this is new line’. We add 1 (or any non-zero number) after {} so that awk prints all lines of file before and after printing the new line of text. If we keep it as 0, then awk will print only the new line of text and not the content of file.

Please note, in this case, awk will not modify input file. If you want to save the result to a file, you will redirect the output of awk command to another file.

$ awk 'NR==3{print "this is new line"}1' data.txt > new_data.txt

$ cat data.txt
hello
good
morning

$ cat new_data.txt
hello
good
this is new line
morning

In this article, we have learnt how to insert text at certain line in text file. You can customize these commands as per your requirement.

Also read:

Django Get Unique Values from Queryset
How to Create RPM for Python Module
What is NoReverseMatch Error and How to Fix It
How to Create RPM for Shell Script
Enable Email Alerts in KeepAlived

Leave a Reply

Your email address will not be published. Required fields are marked *