How to Add a String After Each Line in a File in Linux?

We occasionally need to make changes to files quickly, preferably from the command line. One common task is adding a string to the end of each line of a file. In this article, we'll explore several methods to accomplish this using various Linux commands.

We'll use a sample file called language.txt throughout this article. Let's first create and populate this file

$ touch language.txt
$ cat > language.txt
Hindi
English
Chinese
Spanish
^D

Our goal is to append the phrase "is a good language to learn" to the end of each line in the language.txt file.

Using sed Command

The sed (stream editor) is a powerful built-in utility in Linux that can perform various file operations such as find and replace, search, insertion, and deletion without opening the file.

Example

$ sed -e 's/$/ is a good language to learn/' -i language.txt
$ cat language.txt

Output

Hindi is a good language to learn
English is a good language to learn
Chinese is a good language to learn
Spanish is a good language to learn

The s/$/ text/ pattern matches the end of each line ($) and replaces it with the specified text. The -i flag modifies the file in-place.

Using awk Command

awk is a scripting language that allows us to create simple programs by writing statements that specify text patterns and actions. It was developed by Aho, Weinberger, and Kernighan in 1977.

Example

$ awk '$0=$0" is a good language to learn"' language.txt > languages.txt
$ cat languages.txt

Output

Hindi is a good language to learn
English is a good language to learn
Chinese is a good language to learn
Spanish is a good language to learn

The awk command concatenates each line ($0) with the desired string and saves the output to a new file.

Using perl Command

Perl (Practical Extraction and Report Language) was initially designed to scan text files, extract data, and generate reports. It combines strengths of sed, awk, and shell scripting.

Example

$ perl -pi -e 's/$/ is a good language to learn./' language.txt
$ cat language.txt

Output

Hindi is a good language to learn.
English is a good language to learn.
Chinese is a good language to learn.
Spanish is a good language to learn.

The -pi flags enable in-place editing, so changes are made directly to the input file. The regex s/$/text/ appends text to each line's end.

Using echo with while Loop

The echo command outputs text to standard output. We can combine it with a while loop to read each line and append text.

Example

$ cat language.txt | while read line; do echo "${line} is a good language to learn."; done

Output

Hindi is a good language to learn.
English is a good language to learn.
Chinese is a good language to learn.
Spanish is a good language to learn.

This method reads each line into a variable and uses echo to print the line with the appended text.

Comparison of Methods

Method In-place Editing Performance Complexity
sed Yes (-i flag) Fast Simple
awk No (redirect needed) Fast Simple
perl Yes (-pi flags) Fast Simple
echo + while No (redirect needed) Slower More complex

Conclusion

We explored multiple methods to add a string after each line in a Linux file using sed, awk, perl, and echo commands. The sed and perl methods offer in-place editing, while awk and echo require output redirection. Choose the method based on your preference and specific requirements.

Updated on: 2026-03-17T09:01:38+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements