How would you combine 2 text files with the cat command

How Would You Combine 2 Text Files With the Cat Command

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).

Sponsored

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:

  • Combine the two files into one file using the cat command alphabetically.
  • Combine the two files into one file using the cat command numerically.
See also  How to Install the TeamViewer in Ubuntu 22.04

Syntax:

$ cat [options] filename

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

Options Explanation
-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

How would you combine 2 text files with the cat command 1

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

How would you combine 2 text files with the cat command 2

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.

See also  How to use systemctl in Ubuntu

Output:

Sponsored

Below is the output of the above command.

How would you combine 2 text files with the cat command 3

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

How would you combine 2 text files with the cat command 4

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.

How would you combine 2 text files with the cat command 5

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

How would you combine 2 text files with the cat command 6

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

Output:

Below is the output of the above command.

How would you combine 2 text files with the cat command 7

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.

Karim buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.


Discover more from Ubuntu-Server.com

Subscribe to get the latest posts sent to your email.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply