1. Overview

There are cases when we may want to print each line in a file multiple times using just native commands and programming languages at our disposal. Such a situation could arise when we want to manipulate or substitute characters for each duplicated line we print to the screen. Outputting different variations of the same line could help us detect patterns or anomalies.

In this tutorial, we’ll see how we can use common tools such as awk, sed, Bash, and Perl to duplicate file lines a number of times.

2. Example File

Before we dive into the discussion, let’s check the file that we’ll be working with:

$ cat xfile
zaaiy
cicada
bouhannana

Here, the cat command shows that every line in xfile ends with a newline character (\n), even the last one (bouhannana\n).

3. Using sed

We often use sed for find-and-replace tasks, but we can also instruct it to duplicate file lines.

Since sed reads data in a line-by-line manner, we can use the p command (short for print) to print each line it reads, effectively duplicating content. Let’s check how sed and p work:

$ sed 'p' xfile
zaaiy
zaaiy
cicada
cicada
bouhannana
bouhannana

The sed command has an auto-print feature enabled by default, meaning it automatically prints each line it reads. So, the p command instructs sed to additionally print each line one more time. That’s why sed printed lines twice, even though we only passed one p command.

Let’s disable the auto-print feature and instruct sed to print three lines:

$ sed -n 'p;p;p' xfile
zaaiy
zaaiy
zaaiy
cicada
cicada
cicada
bouhannana
bouhannana
bouhannana

After we suppressed the auto-print, the command repeats each line three times which corresponds with the number of p commands we used. We should keep in mind that when using multiple commands or actions in a single expression we must use a semicolon (;) to separate them.

4. Using awk

Not much different than sed in this context, we can use awk in a similar way to duplicate lines since it also reads files in a line-by-line manner.

Instead of p, we use the print statement of awk:

$ awk '{print;print;print}' xfile

The above program yields the same result as in the example with sed. This works because the awk action applies three print statements on each file line. Naturally, when we explicitly tell awk to perform an action, we should wrap it in curly braces.

4.1. Using awk Default Printing Action

When we use awk to match a pattern, its default action upon finding it is to print that line. So, we can use this behavior to make awk trigger its default printing feature for every line it processes. For instance, since awk evaluates non-zero numbers and strings to true, we can use these conditions as a pattern to match and print every line in our file.

Let’s use the awk command and invoke its default-printing action three times for each line:

$ awk '1;1;1' xfile
zaaiy
zaaiy
zaaiy
cicada
cicada
cicada
bouhannana
bouhannana
bouhannana

The above command evaluates three always-true patterns, so naturally, awk repeats printing (by default) each line in our file three times. Idiomatic awk true conditions like strings or non-zero numbers, always invoke the default print action.

4.2. With for Loop

Another simple way to use awk is with a for loop statement to repeat the printing process.

Let’s duplicate each line three times:

$ awk '{ for (i = 1; i <= 3; i++) print }' xfile

For each line read, the for loop increments the counter variable i by 1 until it reaches 3. In each of these iterations, awk also invokes the print statement to output the current line, resulting in three repetitions of each file line.

5. Using Bash

We can also use Bash to achieve our purpose, even though it’s arguably more complex than the methods above. To do this, we may use the while and for loops together to go over the file and print each line a number of times.

Let’s use a one-liner Bash command to go over the file and print each line three times:

$ while read line; do for i in {1..3}; do echo "$line"; done; done < xfile

In the above code, our command first reads xfile through input redirection (<). Then, the while loop goes through each line in the file and assigns it to the line variable via the read command. Then, the for loop iterates three times, printing the value of line (current line) via the echo command.

6. Using Perl

We may also use Perl to repeat lines in a manner similar to that of sed and awk.

6.1. Using print

We can use Perl’s print function as a duplication factor, meaning we write it as many times as we want to repeat lines.

Let’s write a perl command to invoke print three times on each line:

$ perl -ne 'print;print;print' xfile
zaaiy
zaaiy
zaaiy
cicada
cicada
cicada
bouhannana
bouhannana
bouhannana

The above perl command executes three print functions for each line. This works because the -n option initiates an implicit while loop iterating through xfile, line by line. During each iteration, the -e option executes the code print;print;print on the processed line, causing it to appear three times.

6.2. Using for Loop

Repeating each line in a file can be simpler with Perl than with Bash. Similar to awk, we can use one loop statement paired with the print action to duplicate our file’s lines.

Similar to the awk and bash examples, let’s programmatically duplicate each line three times:

$ perl -ne 'for$i(1..3){print}' xfile

As we know, the -n option starts an implicit loop to go through our content by line. Then, with each iteration, the explicit for loop takes charge of invoking print in its three iterations.

7. Conclusion

In this article, we looked at common methods to repeat each line in a file multiple times.

We also discussed common commands and programming languages like awk, sed, Bash, and Perl, which are part of many Linux distros. Also, we saw how to use the for loop statement in different methods.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.