Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

1. Introduction

When working with files in Linux, we often need to modify text, such as adding a prefix to every line of a file. Whether adding a label, an identifier, or a specific tag to a list of entries, Linux offers several powerful command-line tools to do this efficiently.

In this tutorial, we’ll explore various options to add a prefix to every line of a file, with clear examples and easy steps.

2. Problem Statement

Let’s consider a file file.txt with the following content:

line1
line2
line3

We want to add the prefix “prefix_” to the beginning of each line. Consequently, after processing, the file should look like this:

prefix_line1
prefix_line2
prefix_line3

This task might be necessary in various situations, such as preparing log files, updating configuration files, adding unique identifiers to lists, or any case where we need to label or tag each line for easier processing or identification. Adding a prefix manually would be tedious, especially for large files. Still, Linux offers a range of efficient command-line utilities that can automate this process in just a few steps.

Throughout this article, we’ll explore several approaches to adding the same “prefix_” to each line in file.txt using different Linux tools.

3. Using sed

The sed (Stream Editor) command is a powerful tool for text manipulation. It can be used to add a prefix to each line of a file in a single command.

$ sed 's/^/prefix_/' file.txt

Here, the ^ symbol represents the start of a line, and s is short for substitute. Thus, the “s/^/prefix_/” pattern substitutes the beginning of each line in the file.txt with the string “prefix_”.

This command only outputs the result to the console but does not modify the file.  To make changes to the file, we can use the -i option:

$ sed -i 's/^/prefix_/' file.txt

This is one of the quickest and most straightforward methods. We can use sed when we want a fast solution to modify a file without intermediate steps.

4. Using awk

The awk utility is widely recognized for its flexibility in handling text and data extraction. It efficiently appends a prefix to each line by processing the file line-by-line:

$ awk '{print "prefix_" $0}' file.txt

The above command prints the prefix followed by the current line’s contents. $0 refers to the entire line of text. Here also, we can use the -i inplace option to modify the file directly:

$ awk -i inplace '{print "prefix_" $0}' file.txt

This method proves advantageous when more control or text processing is needed alongside adding a prefix.

5. Using perl

The perl scripting language is widely used for advanced text manipulation tasks. Its concise syntax allows quick adjustments, making it an ideal solution for this task:

$ perl -pe 's/^/prefix_/' file.txt

Here, the -pe flag instructs perl to read each line, apply the substitution, and print the modified output. The “s/^/prefix_/” syntax behaves similarly to sed. Moreover, to modify the file directly, we can use the -i (in-place) option:

$ perl -pi -e 's/^/prefix_/' test.txt

Using perl is suitable for large files or cases where more advanced text processing may be required later in the workflow.

6. Using a while-read Loop

A while-read loop, combined with echo, provides a more interactive method to add prefixes to each line of a file.

$ while IFS= read -r line; do
    echo "prefix_$line"
  done < file.txt

This loop reads each line from file.txt and prepends “prefix_” using echo. The IFS= and read -r ensure that each line from file.txt including leading/trailing whitespace is entirely read into the variable line.

To save the changes into a file, we can redirect the modified output to temp_file and use the mv command to replace the original file.txt with the modified file:

$ while IFS= read -r line; do
     echo "prefix_$line"
  done < file.txt > temp_file && mv temp_file file.txt

We can also use printf instead of echo to get the same result:

$ while IFS= read -r line; do
     printf "prefix_%s\n" "$line"
  done < file.txt

This approach offers flexibility for cases requiring real-time processing or more customized control over file manipulation.

7. Using xargs

Another powerful tool, xargs, processes the file content and allows the efficient addition of prefixes through batch processing:

$ xargs -I{} echo prefix_{} < file.txt

This command uses xargs to process each line from the file, appending the prefix through the echo command. The -I{} tells xargs to use {} as a placeholder for each line from the file.txt. It replaces {} with the current line read from file.txt during execution.

To modify the file, we can use:

$ xargs -I{} echo "prefix_{}" < file.txt > temp_file && mv temp_file file.txt

This method is useful when integrating multiple commands or handling large files that need quick processing.

8. Using vi/vim

Finally, if we prefer to work within an editor, vi, and vim offer an easy way to append a prefix in command mode:

$ vi -c '%s/^/prefix_/' -c 'wq' file.txt

This command opens vi in command mode, applies the substitution, and saves the file. The wq ensures that we save the changes in file.txt before exiting the editor.

This method is convenient if we already use vi/vim as our primary text editor.

9. Conclusion

In this article, we looked at various methods to add a prefix to each line in a Linux text file. Each method offers unique advantages, depending on the complexity of the task and the level of control required.