sed Add Line to End of File

sed add line to end of file

Using sed with a command:

Use sed with the a command to add a line to the file’s end in Bash.

In this example, the sed command is used with the a option to add a line This is the last Line to the end of the file myFile.txt. In Bash, sed is a stream editor. It is used for text transformation and manipulation.

Here, $ represents the last line of the file and a represents the append function. So, the command, '$a\This is the last Line' instructed the sed command to append the line after the \ sign at the end of the file.

On execution of above command, no output is displayed on the console. However, the line This is the last Line' is appended at the end of the myFile.txt. Here, i is used to modify the file in place; in other words, it is used to edit the file directly. You can check the file to verify.

Now let’s assume a scenario where you want to keep the original file unchanged and save the modified content to the new file; for that, consider the below example:

In this example, the sed command appended the line This is the last Line to the end of myFile.txt. The > operator redirects the output of the sed command to a new file called modified_file.txt (if the file does not exist, it will be created). The original file myFile.txt remains unchanged.

Note, on the execution of the above bash script, a new file named modified_file.txt is created in the current working directory containing the modified content of the myFile.txt file. You can see the content of modified_file.txt using the cat modified_file.txt command.

Now, If you want to add multiple lines at the end of the file, consider the below example:

The above script adds multiple lines at the end of the file myFile.txt using the a command in sed. Each line to be added is separated by a backslash \ and a line break (\n). On execution of the above bash script, the given lines are appended to the end of the file myFile.txt.

Using sed with s command:

Use sed with the s command to add a line to the end of the specified file in Bash.

In this example, sed is used to substitute the end of the given file ($) with a new line. Here, the s/$/ part indicates that we want to substitute (s) at the end of the file, and \nThis is a new line/ specifies the replacement text, where \n represents a line break.

So, on the execution of the above bash script, the end of the file myFile.txt will be replaced with the line This is a new line. You can use the cat myFile.txt command to check the changes in the file.

Using sed with r Command

Use sed with the r command to add a line to the end of a file in Bash.

In this script, the r read command is used with sed to read and append the line from a separate file. Here, the read command (r) reads the content of the myFile.txt file and appends it at the end of the newFile.txt file. If the mentioned file does not exist, it will be created.

Let’s see the content of the newFile.txt to verify whether the line is added at the end of the file.

We can see the line This is a new line added at the end of the newFile.txt.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *