The CAT command in Linux also known as concatenate is a largely used command to create files, view the file content, direct output in the terminal window, and concatenate these files.
The use case of the CAT command is not only limited to this but it is also used to combine text from two or multiple files into a common file.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Linux Command queries.
In this context, we shall look into different methods to combine text files using the CAT command in Ubuntu 20.04 system. Also you will see a few examples to help you better understand the proper usage of the CAT command.
1. Merge text from multiple text files.
2. Merge text from multiple text files and save the output file in a sorted way to another file.
3. Append text from one text file to another text file.
4. Append text from the Terminal window directly into a text file.
To merge the text from multiple files, we have used two different sample text files. They are named textfile1.txt and textfile2.txt respectively.
The files have one-liners in them and are located in the Desktop directory.
Let's access the terminal window by using the Ctl+Alt+T shortcut.
When the terminal window is opened, access the Desktop directory of the system.
Once done, type the following command in the terminal window to display the text in the output:
$ cat [textfile1.txt] [textfile2.txt]
To combine the output of the two files in the third file, type the following command:
$ cat [textfile1.txt] [textfile2.txt] > [textfile3.txt]
To print the output text file type:
$ cat textfile3.txt
The text of the two files is now present in the third file.
Also note that in case if the textfile3.txt (file in which you are printing the output) already has the content then this command will overwrite it.
Now, let's try out the case where we have two text files that have some text. We want to sort the text from these files in alphabetical order in another file using the CAT command.
First, let's check the text in these files.
We will create a new textfile3 and combine the text of the two files i.e. textfile1 and textfile2 using:
$ cat [textfile1.txt] [textfile2.txt] > [textfile3.txt]
Combined output can be seen as displayed below using:
$ cat textfile3.txt
To sort them out alphabetically, we will create another file textfile4.txt, and use the following command:
$ cat textfile1.txt textfile2.txt textfile3.txt | sort > textfile4.txt
On displaying the output you will see the sorted text values from the file.
$ cat textfile4.txt
This way the text from the file can be sorted easily.
The cat command can be used to append text from one file to another. This method can be chosen smoothly because it does not mess up with the file content.
To append the text, use the following command:
$ cat sourcetextfile.txt >> destinationfile.txt
The appended output file can be displayed using:
$ cat textfile3.txt
To append text via the command line at the end of an already existing text file, use the following command:
$ cat >> textfile.txt
As soon as you enter this text in the command line, a cursor will appear.
Here you can add the text to be appended into the text file.
Once done, use the Ctl+D shortcut to save the changes.
On accessing the file, you will see the text appended.
This article covers the detailed examples used for combining the text files in Ubuntu 20.04 system using the CAT command.
Users can easily use these methods to merge text from multiple files and sort it into another file.
They can also append text from one file to another file using the Ubuntu terminal.