- Append text to file using sed
- 4 Answers 4
- Use CAT Command to Combine Text Files in Ubuntu 18.04
- Example 1: Merging text from three files to another text file
- Example 2: Merging text from three files, and saving the output to another file in alphabetical order
- Example 3: Appending text from one text file to another
- Example 4: Appending text from the Terminal directly to a file
- How to append one file to another in Linux from the shell?
- 8 Answers 8
Append text to file using sed
How can I write text to a file using sed? More specifically I would it add null variables to my blank text file that was created using touch. The syntax of sed is very confusing to me.
Please show us what you’ve tried, along with your results, and an example of the output you’re expecting.
4 Answers 4
sed -i "$ a some text" somefile.txt
It’s useful to know how to actually do this with sed. Sometimes you want to use sed to make multiple changes to a file, including adding a line to the end, and it’s nice to be able to do that in a single command.
specially useful since it can be used with sudo , while sudo echo > file would be doing a sudo echo and then redirecting the output as non-sudo to file
This is a syntax error on MacOS and other *BSD platforms. The a command is not properly standardized by POSIX so whether or not it works on an empty file is probably platform-dependent. The syntactically correct sed -i » ‘a$\ (newline) some text’ somefile.txt does not actually modify an empty file on MacOS Big Sur, like @doak already remarked (presumably regarding Linux).
This adds the text on a new line, what if I want to append it inline? Do I really have to use s/$/. / ?
If you’re just appending text to the end of the file then you wouldn’t use sed in the first place.
echo "some text" >> somefile.txt
I’ve been using an example to change variables in a file, I thought maybe you could add them too. This is MUCH simpler.
Adding a late post because while the accepted answer is the simplest solution to this problem, the question actually highlights a couple of common situations:
In short, you can’t touch , then edit a file with sed . Sed doesn’t work on empty files, but occasionally you need to do the equivalent of
sudo echo "some text" >> somefile.txt
sudo doesn’t like the redirect, but there are workarounds:
echo "some text" | sudo tee --append somefile.txt
or if pipes are not an option either, or you simply must use sed, or you just like complexity:
dd if=/dev/zero count=1 bs=1 of=somefile.txt sed -i '$ a some text' somefile.txt sed -i '1 d' somefile
On some systems,. you might have to use sed -i -e ‘$ a .
This sounds like an XY Problem to me. You might get better or more useful answers if you ask about what problem you’re trying to solve, rather than asking for help implementing a particular solution.
But if you absolutely must do this in sed, you can use sed’s r command to read a file. For example:
[ghoti@pc ~/tmp]$ cat one RED BLUE [ghoti@pc ~/tmp]$ cat two green yellow [ghoti@pc ~/tmp]$ echo ">> start"; sed '$r two' one; echo ">> end" >> start RED BLUE green yellow >> end [ghoti@pc ~/tmp]$
The sed command $r two causes sed to slurp in the file named «two» after processing the last line of input (which in this case is from the file «one»).
Note that this merges one file into the stream of another file, but sed’s «standard output» ( stdout ) is what contains the full/combined stream. You can redirect that output to a file per Ignacio’s answer if that’s what you want.
Use CAT Command to Combine Text Files in Ubuntu 18.04
The CAT command in Linux is not only helpful in creating text files, displaying their contents, but also in merging text from two or more text files. The merged text can then be saved to another text file. In this tutorial, you will learn the use of the CAT command to combine text from two or more files into a single one. This will help you in achieving a power user status on Ubuntu from an average beginner. We have run the commands mentioned in this tutorial on an Ubuntu 18.04 LTS system.
Let us present a few examples in this article which will help you in understanding the proper usage of the CAT command in the following four scenarios:
- Merging text from multiple text files to a single text file.
- Merging text from multiple files, and saving the output to another file in alphabetical order.
- Appending text from one text file to another.
- Appending text from the Ubuntu Terminal directly to a text file.
Note: It is a good practice to backup important files before altering their contents.
Example 1: Merging text from three files to another text file
We have created three sample text files on our system by the name of textfile1.txt, textfile2.txt, and textfile3.txt. All of these files contain a line of text. The following use of the CAT command will display the text from all of these files in a single output.
Open the Ubuntu Terminal by either pressing CTRl+Alt+T or through the Dash, and then enter the following command:
$ cat [file1.txt] [file2.txt] [file3.txt]
In the following image you can see how the output from my three text files is printed as a single merged output:
Linux allows you to print the output of a command to a file by using the following syntax:
Let us make use of this command and the cat command to save the text from three different text files to a new text file:
$ cat [file1.txt] [file2.txt] [file3.txt] > [file4.txt]
In the following image, I am saving the merged text from my three files to a new file textfile4.txt; I am then printing the contents of the new file to the screen for you to view:
Please remember that if the destination text file already exists in your system, its contents will be overwritten.
Example 2: Merging text from three files, and saving the output to another file in alphabetical order
Suppose you have three text files; each containing some text. You want to merge the text from all three and save the output to a fourth file, but in alphabetical order. This is how you will do it:
$ cat [file1.txt] [file2.txt] [file3.txt] | sort > [file4.txt]
In the following image, you can view the text from each of my text files. If I simply combine the text to a new file textfile4.txt, the output will be as follows:
However, I want an alphabetically sorted output to be printed to my text file, so I will use the following command:
$ cat textfile1.txt textfile2.txt textfile3.txt | sort > textfile5.txt
You can see how my newly created textfile5.txt contains merged and sorted text from my three source files.
Example 3: Appending text from one text file to another
The cat command can also be used to append text from a source file to a destination file without messing up with the contents of the later.
Here is a sample destination file:
Here is a sample source file:
The syntax for appending text:
$ [sourcefile.txt] >> [destinationfile.txt]
Here is how my destination file looks after I append the text from my source file to it:
Example 4: Appending text from the Terminal directly to a file
If you want to append some text, from the command line, at the end of an already existing text file, you can use the following syntax:
After entering this command, a cursor will appear for you to enter the text you want to add to the specified file. Enter the text and press Ctrl+D. Your entered text will be appended at the end of the file without disturbing its already existing contents.
You can see this text added to the file in the following image:
We hope that the detailed examples described in this article along with the syntax of the cat command in each case will help in merging the contents of multiple files into a single one. Moreover, you can excel sorting and appending of text not only from one file to another but also directly from the Ubuntu Terminal.
How to append one file to another in Linux from the shell?
I have two files: file1 and file2 . How do I append the contents of file2 to file1 so that contents of file1 persist the process?
8 Answers 8
@BijayRungta: It sounds like you answered your own question. You’d pre-pend sudo to the cat command (and enter credentials if prompted).
you need to . chmod 777 /etc/default/docker to give yourself write permissions on that file — be sure to restore the old file permissions once done
@Sigur: Unless there’s a way to direct the output to two files at once, it would involve two invocations of the command.
@Sigur or have a look at the tee program: cat 1 | tee -a 2 3 . You can put as many files as you like after the —append (or -a for short) switch.
The >> operator appends the output to the named file or creates the named file if it does not exist.
This concatenates two or more files to one. You can have as many source files as you need. For example,
Update 20130902
In the comments eumiro suggests «don’t try cat file1 file2 > file1 .» The reason this might not result in the expected outcome is that the file receiving the redirect is prepared before the command to the left of the > is executed. In this case, first file1 is truncated to zero length and opened for output, then the cat command attempts to concatenate the now zero-length file plus the contents of file2 into file1 . The result is that the original contents of file1 are lost and in its place is a copy of file2 which probably isn’t what was expected.
Update 20160919
In the comments tpartee suggests linking to backing information/sources. For an authoritative reference, I direct the kind reader to the sh man page at linuxcommand.org which states:
Before a command is executed, its input and output may be redirected using a special notation interpreted by the shell.
While that does tell the reader what they need to know it is easy to miss if you aren’t looking for it and parsing the statement word by word. The most important word here being ‘before’. The redirection is completed (or fails) before the command is executed.
In the example case of cat file1 file2 > file1 the shell performs the redirection first so that the I/O handles are in place in the environment in which the command will be executed before it is executed.
A friendlier version in which the redirection precedence is covered at length can be found at Ian Allen’s web site in the form of Linux courseware. His I/O Redirection Notes page has much to say on the topic, including the observation that redirection works even without a command. Passing this to the shell:
. creates an empty file named out. The shell first sets up the I/O redirection, then looks for a command, finds none, and completes the operation.