Spread the love
Android file management: An easy-to-follow guide | Computerworld

A file is a data storage resource in a computer that is mainly recognized by its filename. Sometimes we need the scattered data of two or more than two files in one place in an organized way. There are many commands available in Linux to combine the multiple file data in one file, but this Article is all about combining data of two files in one using the “cat’ command on Ubuntu 20.04(Linux Operating System).

As the name suggests, The “cat” command is abbreviated as “concatenate” that is used to combine the data. We can use the cat command to create single or multiple files, view their contents, merge files, and show output to a terminal screen or redirect it to files. In this article, the cat command will be used to merge data from two files into one file in an organized manner.

Combine 2 Text Files with the Cat Command

Below mentioned are the ways to organize the two files into one using the cat command which are discussed in detail below:

Syntax:

$ cat [options] filename

Options will help in formatting the display content of the file.

OptionsExplanation
-AEqual to -vET
-bDisplay all non-empty output lines with numbering
-eEqual to -vE
-EPut $ at the end of each output line
-nDisplay all output lines with numbering
-sRepeated empty output lines are suppressed.
-tEqual to -vT.

The “cat” command mostly comes pre-installed in new Ubuntu versions, but if it is not installed run the below-mentioned command to install it:

$ sudo apt install coreutils

Combine the two files into one file using the cat command alphabetically

To combine and merge the data of two files into one alphabetically, use the sort command with the cat. Below mentioned command will combine the data of “linux1.txt” and “linux2.txt” and sort the data alphabetically in file “alpha_linux.txt”.

$ cat linux1.txt linux2.txt | sort > alpha_linux.txt

Standard Redirect Symbol (>) is used before the filename to insert content into a file, but it will overwrite the file if some content existed previously. Use “>>” to avoid the overwrite. Sort is a command to organize data according to the mentioned standard.

Output:

Below is the output of the above command.

Combine the two files into one file using the cat command numerically

To combine the two files into one numerically, use the “-n” option with the sort and cat command. This option is only beneficial if your file’s lines begin with line numbers. Please remember that “03” would be less than “2” in the default manner. The below-mentioned command will merge and sort “linux1.txt” and “linux2.txt” numerically in ascending order of line numbers into the file “num_linux.txt”.

$ cat linux1.txt linux2.txt | sort –n > num_linux.txt

The standard redirect symbol (>) is used before the filename to insert content into the file, but it will overwrite the file if some content already exists. Use “>>” to avoid the overwrite. Sort is a command to organize data according to the mentioned standard.

Output:

Below is the output of the above command.

The below-mentioned command will merge and sort “linux1.txt” and “linux2.txt” numerically in reverse order of line numbers into the file “num_linux.txt”.

$ cat linux1.txt linux2.txt | sort –nr > num_linux.txt

-nr will perform sorting in reverse order (descending order).

Output:

Below is the output of the above command.

Conclusion

Combining the content of files into one file in an organized manner is the utility that is provided by the Linux operating system through multiple commands. This article merges the content of files through the cat command; different techniques are used in this article, like organizing the two files into one file alphabetically and numerically. You can follow any of the techniques that are appropriate for you.

Another interesting article may be: Simple Ways To Restart Your Ubuntu Linux

Leave a Reply

Your email address will not be published.