1. Overview

We sometimes need to quickly make changes to files, preferably from the command-line itself. Adding a string to the end of each line of a file is one such change.

In this tutorial, we’ll look at multiple methods to do this using sed, awk, echo, and perl.

2. Problem Statement

Throughout this article, we’ll be working with the following sample file named test.txt file:

$ cat test.txt
Python
Java
JavaScript
PHP

We’ll see how we can add the string ” is a great programming language.” to the end of each line in our test.txt file using different methods.

3. Using sed

sed (stream editor) is a powerful built-in utility in Linux. We can use it to perform functions like find and replace, search, insertion, and deletion on files.

We can use sed to edit files without even opening them, which makes it an easy and quick way to perform different file functions.

Let’s run the sed command on the test file:

$ sed -i s/$/ is a great programming language./ test.txt
Python is a great programming language.
Java is a great programming language.
JavaScript is a great programming language.
PHP is a great programming language.

Let’s break down this command to understand more about it:

  • -i: instructs sed to edit the file in place
  • s: for substitution
  • /: for substitution based on regular expression
  • $: regular expression for matching end of line
  • ” is a great…”: represents the string we want to add

But our results are only printed on standard out. To save the changes, we need to modify our command:

$ sed -i s/$/ is a great programming language./ test.txt > test_sed.txt

This saves our changes to the test_sed.txt file.

4. Using awk

awk is a scripting language included in Unix and most Unix variants. It is abbreviated from the names of its developers – Aho, Weinberger, and Kernighan – who built it in 1977.

awk enables us to write small programs in the form of statements that define text patterns to be searched for in each line of our file, and an action to be taken once a match is found.

Let’s type in the following command to add our string:

$ awk '{print $0, " is a great programming language."}' test.txt
Python is a great programming language.
Java is a great programming language.
JavaScript is a great programming language.
PHP is a great programming language.

Let’s understand the command:

  • print: used to output selected data from the input file.
  • $0: regular expression used to match the whole line.
  • ” is a great…”: represents the string we want to add at the end of each line.

This command outputs the result to standard output (STDOUT).

Let’s modify it to save our changes to another file:

$ awk '{print $0, " is a great programming language."}' test.txt > test_awk.txt

5. Using echo

As we know, the echo command writes text to the standard output (STDOUT).

While the behavior of the echo command varies slightly from shell to shell, we’ll cover the bash built-in version here.

Let’s combine the cat command and run echo:

$ cat test.txt | while read line; do echo ${line}$" is a great programming language."; done
Python is a great programming language.
Java is a great programming language.
JavaScript is a great programming language.
PHP is a great programming language.

As before, let’s break this down:

  • cat: we used cat to read our file first.
  • | pipe: we used this to combine cat with an additional command.
  • while…do: we used while to loop through each line in our file.
  • done: we used this to terminate our function once it’s done.

However, our result is only printed to standard out (STDOUT), to save our changes.

Let’s save the changes to a new file called test_echo.txt:

$ cat test.txt | while read line; do echo ${line}$" is a great programming language." >> test_echo.txt; done

6. Using perl

perl (Practical Extraction and Report Language) is a language originally optimized for scanning arbitrary text files, extracting information from them, and generating reports based on the input data. It combines some of the features of sed, awk, and sh, making it easier and familiar for us to whip up quick solutions to common problems.

Let’s run the perl command:

$ perl -pi -e 's/$/\ is a great programming language./' test.txt

Let’s take a close look to understand more:

  • -pi: we used this tag to print and save our changes.
  • -e: we used this tag so that we can execute perl code from the terminal instead of creating a file.
  • s/$/: represents a regular expression that targets the end of each line on our file.

The command above doesn’t show the result on standard out. However, we can use the cat command on our input file itself to look at the changes. Since perl directly makes changes on the input file, we don’t need to create a new file.

7. Conclusion

In this article, we learned how we can add a string to the end of each line of a file in Linux. We used sed, awk, echo, perl, and other commands to achieve this. Since the methods, we looked at work similarly, which method we choose is a matter of preference.