How to use Sed to add a new line to the end of each line

Sed stands for a stream editor, which is used for basic editing operations on text coming from a file, or input that is passed directly to sed from another command. Similar to input criteria, sed can process an input file and pipe the result to another program (since it accepts input directly from other programs). It can be used to keep track of the same word that is used in different ways in the document, and users can set them for better understanding. Sed is based on a text editor, but it can also be used to write complex scripts. However, the basic operations that the sed command can perform are listed below:

  • To print a line
  • Find and replace words in a string or text file
  • Delete a line containing a specific word
  • Add line / text after any line (by specifying line number)
  • Add a line before the start of each line or before any specific line

The command sed utility in Ubuntu does several things; our tutorial today will focus on adding text at the end of each line in any text file.

So, before reading this tutorial, let’s go over the basic syntax of the sed command:

Syntax

sed [options] command [file to edit]

The options to the sed command are used to get output in several formats; for example, the -i option is used to save changes (caused by the sed command) to the original file. The “command” portion of this syntax contains the basic sed operation that you want to perform with that command (edit, delete, or print a line). Finally, the “file to edit” consists of the name of the file to which the sed command is applied.

How to install sed on Ubuntu

Most Linux distributions have sed built in; you can get the sed version to check if it is available on your system or not; write the following command in terminal to check for sed on your Ubuntu:

$ sed --version

If sed is not available on your Ubuntu; you can install it using your package manager by running the command given below:

How to add a new line to the end of each line

Since the operation will be performed on a specific text file; So, you have to create a text file and add text to it, or you can also apply sed to any existing text file (make sure the file does not contain any important information, otherwise you may lose information during testing):

We created a text file “test.txt” on our desktop (you can use any directory) and added a few lines of text to it. Let’s get the contents of “test.txt” to the terminal using the command written below:

$ cat test.txt

We will now use the sed command to add the line “You are using an Ubuntu terminal” to each line of the “test.txt” file; So, the command below will help you accomplish this action:

Note that the keyword “a” is used here to add the text after it to each line of the “test.txt” file:

$ sed ‘Вы работаете в терминале Ubuntu’ test.txt

You will notice that the result is printed to the terminal, but the original “test.txt” file remains unchanged; if you want the changes to be made to the original file as well; you must use the “-i” parameter as we did with the following command:

$ sed -i ‘Вы работаете в терминале Ubuntu’ test.txt

In addition to appending to each line, the sed command gives you the ability to append text to any specific line; for example, the following command will only add text to line # 3, and the changes will be made to the original file as well:

$ sed ‘3a sed - это универсальная утилита командной строки’ test.txt

You can also save the result of any sed command to another file; for example, the command below will save the result in a new text file “output.txt”.

$ sed '3a sed - это универсальная утилита командной строки' test.txt > output.txt

Conclusion

Sed is a command line utility known as a stream editor that can perform some basic operations on a file such as find, replace, insert, or delete. Apart from these basic operations, it can also be used for complex scenarios: hence it is said that a novice user may hesitate to learn this. In fact, sed is easy to learn and implement at a basic level, and new users should try it in order to perform the above operations. Knowing the importance of sed, we put together this guide to demonstrate how to add a new line or words to the end of each line. Moreover, users can also perform this task for a specific line number by specifying the number in the command.

Related Posts