1. Overview

Let’s suppose we need to insert some data at a specific line of a large file. Because of its size, it’d take a lot of time to open it in an editor and add it manually. Also, it might be hard to scroll to the correct line.

In this tutorial, we’ll see how to insert text at a specific line of a file using Linux commands. We’ll be looking at ed, sed, awk, perl, and ex commands that allow us to modify and manipulate text files. We’ll also see how to achieve this using a combination of the head, tail, and echo commands.

2. Using ed

Let’s suppose we have a file named File1, which contains:

$ cat File1
Line #1
Line #2
Line #3
Line #4

And we want to insert some text in the third line of File1. Let’s see how it’s done using the ed command:

$ ed File1
32

We can see that ed is displaying the number of characters in the file. Notice that there is no prompt after 32. This is because ed is an interactive line editor. So, ed waits for us to enter further instructions.

By default, ed points to the last line of the file, and each command in ed will work on the current line. Therefore, ed applies the command to the last line by default. Now, let’s specify a line number and a command:

3i
New Line with ed

Let’s see what 3i means. The number 3 specifies the line where we want to insert the text. Then,  I enable the insert mode. The next line is the text that we want to insert. We can add one or several lines.

After all the inputs, we should save the changes and exit:

.
w 
q

Let’s see what these arguments are. The ‘.’ finishes input mode. Then we wrote the changes with entering w. Finally, we inserted q to quit ed. Let’s use the cat command to see the content of the modified file:

$ cat File1
Line #1
Line #2
New Line with ed
Line #3
Line #4

Note that instead of entering data in this interactive shell manually, we can make a script of our edits. And then, place them in a separate file. Let’s write this script:

$ cat script.ed
3i
New Line with ed
.
w
q

The script.ed script has all the instructions we’ve used. Now, we can redirect the script as an input to the ed editor:

$ ed File1 < script.ed 
32
49

Let’s see what these numbers in the output are. The number 32 is the number of characters in File1 before edits, and 49 is the number of characters in the file after applying the script. In this way, we can know for sure that our data is inserted correctly.

3. Using sed

The sed command is a powerful utility for editing files in Linux. It offers lots of operations on files. In this section, we’re going to insert text in the third line of File1 as we did with ed, but this time using GNU sed. The sed command was created based on ed, but unlike this, it cannot be used interactively. Indeed, we’ll set the modification commands in the same line:

$ sed '3 i New Line with sed' File1
Line #1
Line #2
New Line with sed
Line #3
Line #4

Let’s see what we did in the single quotations. First, we added the line number for insertion. The i, same as in ed, enables sed’s insert mode. Finally, we set the content of the new line. Note that the space between any element in quotations is not necessary. For instance, we can change the phrase in the example to ‘3iNew Line with sed’ and get the same result as above.

Now, if we print File1, we’ll see that the file hasn’t changed yet:

$ cat File1 
Line #1 
Line #2 
Line #3 
Line #4

Changes with ed will save directly to the input file, but with sed, we need an option to save the result to the primary file. By using the -i option for sed, we can apply changes directly to the file. If we don’t use this option, sed prints the changes only on the terminal. Let’s add -i to our previous command:

$ sed -i '3 i New Line with sed' File1

Note that the -i option is only available for GNU sed.

If we take a look at File1‘s content, we can see that the new line was inserted successfully:

$ cat File1 
Line #1 
Line #2 
New Line with sed 
Line #3 
Line #4

If we don’t want to change the input file, we can use redirection to save the output to a new file. In this case, we don’t need to use the -i option:

$ sed '3 i New Line with sed' File1 > sed_out

If we check out the content of the sed_out file, we can see that the result is the same as before:

$ cat sed_out
Line #1 
Line #2 
New Line with sed 
Line #3 
Line #4

It should be noted that sed is the most common way of inserting. Nevertheless, it’s good to know other alternatives.

4. Using awk

awk is a non-interactive programmable editor that reads inputs line by line. By default, awk reads one line at a time. Moreover, it can also read a record containing multiple lines.

awk provides a programming language like C to write scripts for line editing. Like sed, awk is originated from ed. Similar to the previous examples, let’s add our text to the third line of file1:

$ awk 'NR==3{print "New Line with awk"}1' File1
Line #1
Line #2
New Line with awk
Line #3
Line #4

Inside the quotation marks, we have condition {action} pair. In the condition section, we use the awk NR (number of records) variable to specify the line number. The action section should be in braces ({}) and will only apply to the line addresses we mentioned in the condition. To process, awk checks the condition, and if true, it will execute the action section.

In awk, there is no option like the sed -i option to save the result to the input file. We can redirect the stdout (>) to save the result in a new file:

$ awk 'NR==3{print "New Line with awk"}1' File1 > awk_out

Notice we specified the line number with ==. Also, note that the 1 at the end of the quotation is equivalent to true. It could be any non-zero number instead. We should put any non-zero number after the action section in braces so that awk performs the default action, print, for every line. Otherwise, awk considers 0 or false by default and just prints the new line and ignores the file1‘s content. Let’s print the awk_out file:

$ cat awk_out
Line #1
Line #2
New Line with awk
Line #3
Line #4

Notice that with awk we can’t save the results back to the input file. But since its release 4.1.0, the gawk (GNU awk) command supports the -i option.

5. Using perl

perl is a non-interactive command like sed and awk. For this reason, we need to use a combination of options and if statements to insert a new line into the File1. Let’s see how it works:

$ perl -e 'print "New Line with perl" if $. == 3' File1

The option tells perl to execute the commands in single quotations. In quotations, we ask perl to print the phrase “New Line with perl” if it reaches the third line. The search in the if pattern is done with the $. A symbol that has the current line number.

Unlike other commands, perl does not print the result by default. Therefore, we can add the -p option to print all the lines of File1. This allows us to check whether the changes are made correctly or not:

$ perl -p -e 'print "New Line with perl" if $. == 3' File1
Line #1
Line #2
New Line with perlLine #3
Line #4

We can see that perl almost achieves the expected result, but it doesn’t add a new line (\n) after the inserted text. To accomplish this, we could add \n to our phrase and changed it to “New Line with perl\n”. But we can also use the perl‘s -l option to add \n at the end of the new line automatically:

$ perl -l -p -e 'print "New Line with perl" if $. == 3' File1
Line #1
Line #2
New Line with perl
Line #3
Line #4

Like sed, perl has the -i option to save the changes in place:

$ perl -i -l -p -e 'print "New Line with perl" if $. == 3' File1

Now, let’s use output redirection to save the modifications to a new file:

$ perl -l -p -e 'print "New Line with perl" if $. == 3' File1 > perl_out

Finally, let’s see the resulting file:

$ cat perl_out
Line #1
Line #2
New Line with perl
Line #3
Line #4

6. Using ex

The ex utility is a line-oriented interactive text editor, like ed, that is derived from the vi. We’re going to use ex for adding a line to file1. So, let’s first open the file with ex:

$ ex File1

When we open a file with the ex, a new window will open, and we should add our commands in that window. So, there will be no prompt. In the open window, we see some lines:

"File1" 4L, 32C 
Entering Ex mode. Type "visual" to go to Normal mode. 
:

These lines are some information about the input file. The first line includes the number of lines and the number of characters in the input file. The second line says that if we type visual, we can see the file content and edit the file manually. Note the semicolon in the third line. The editor puts a semicolon at the beginning of each command line.

For insertion, the syntax of ex is similar to the ed editor. Let’s see how it works:

3 insert 
New Line with ex 
. 
xit

In the first line, we added the line number for insertion. Secondly, we specified the insert action in the same line. Then, in the next line, we added the phrase we wanted to insert. In the end, we need to put a dot (.) to end the input mode and then add xit to save the changes and quit the editor.

With ex, the result will save in place. Because it is an interactive editor, it lets us change the input file and then save the changes with xit. So, if we cat File1, we can see that ex inserted the data successfully:

$ cat File1 
Line #1 
Line #2 
New Line with ex 
Line #3
Line #4

7. Using a Combination of head and tail

Let’s use a combination of the head, tail, and echo commands to insert a new line:

$ { head -n 2 File1; echo "New Line"; tail -n +3 File1; }
Line #1
Line #2
New Line
Line #3
Line #4

First, we used braces ({}) to group the commands. Secondly, we used a semicolon at the end of each command to specify its ending. Note that we need to insert a new line into the third line. So, we used the head command to print the first two lines of File1. Then, we used the echo command to print the new line. And in the end, we used the tail command to print the rest of the input file, starting by line 3. Let’s save the output in a new file:

$ { head -n 2 File1; echo "New Line"; tail -n +3 File1; } > out

Finally, we redirected all the changes to the out file. If we look at the output file, we can see that we have the same result as before:

$ cat out
Line #1
Line #2
New Line
Line #3
Line #4

8. Conclusion

In this tutorial, we introduced six ways of inserting a line to the given file. With sed, awk, and perl, we don’t need to open the file for editing. So, the speed of modification with them is faster than with the rest of the commands.

guest
0 Comments
Inline Feedbacks
View all comments