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


Introduction

We occasionally need to make changes to files quickly, preferably from the command line. One example is adding a string to the end of each line of a file.

In this article, we'll look at several ways to accomplish this using various Linux commands.

The sample file language.txt that follows will be used throughout this article −

pi@TTP:~ $ touch language.txt

Example

A file will be created with the name of language.txt.

pi@TTP:~ $ cat > language.txt

With the help of cat command with > we can insert data into the file. If we open the file language.txt you will get the following output.

Output

Hindi
English
Chinese
Spanish

We'll examine various techniques for appending the phrase "is a good language to learn." to the end of each line in our language.txt file

By Using sed

In Linux, sed (stream editor) is a powerful built-in utility. It can be used to perform file functions such as find and replace, search, insertion, and deletion.

Example

Let’s take a simple example to accomplish the given task using sed command −

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

With the help of sed command, you will add a string after each line in the file, you can see the output by using cat command.

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

It is simple and quick to carry out many file functions using sed because we can alter files without even opening them.

By Using awk

With the help of awk, we can create simple programs by writing statements that specify text patterns to be looked for in each line of our file and actions to be performed when a match is discovered.

The majority of Unix versions and Unix include the scripting language awk. It is short for Aho, Weinberger, and Kernighan, the developers who worked on its construction in 1977.

Example

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

With the help of above command, we can add a string after each line in the file and the output will be saved in languages.txt file. You can view the content of languages.txt by using cat command.

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

By Using perl

The initial purpose of perl (Practical Extraction and Report Language) was to scan arbitrary text files, extract data from them, and create reports using the information. It combines some of the strengths of sed, awk, and sh, making it simpler and more comfortable for us to come up with rapid fixes for typical issues.

Example

Lets execute the perl command −

$ perl -pi -e 's/$/\ is a good language to learn./' 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 output of the command is not visible on standard out. To see the changes, we may use the cat command on our input file directly. We don't need to make a new file because perl makes changes straight to the input file.

By Using echo

The echo command, as we all know, outputs text to the standard output (STDOUT).

While the behaviour of the echo command differs slightly between shells, we'll focus on the bash built-in version here.

Let's combine the cat command with the run echo command −

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.
Spainsh is a good language to learn.

Conclusion

We learned in this article how to add a string to the end of each line of a file in Linux. To accomplish this, we used sed, awk, echo, perl, and other commands. Because the methods we examined work similarly, the method we select is a matter of personal preference.

Updated on: 21-Nov-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements