Categories: TutorialsUbuntu

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.

Syntax:

$ cat [options] filename

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

Options Explanation
-A Equal to -vET
-b Display all non-empty output lines with numbering
-e Equal to -vE
-E Put $ at the end of each output line
-n Display all output lines with numbering
-s Repeated empty output lines are suppressed.
-t Equal 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:

Sponsored

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.

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.

Ubuntu Server Admin

Recent Posts

Building RAG with enterprise open source AI infrastructure

One of the most critical gaps in traditional Large Language Models (LLMs) is that they…

9 hours ago

Life at Canonical: Victoria Antipova’s perspective as a new joiner in Product Marketing

Canonical is continuously hiring new talent. Being a remote- first company, Canonical’s new joiners receive…

1 day ago

What is patching automation?

What is patching automation? With increasing numbers of vulnerabilities, there is a growing risk of…

2 days ago

A beginner’s tutorial for your first Machine Learning project using Charmed Kubeflow

Wouldn’t it be wonderful to wake up one day with a desire to explore AI…

3 days ago

Ubuntu brings comprehensive support to Azure Cobalt 100 VMs

Ubuntu and Ubuntu Pro supports Microsoft’s Azure Cobalt 100 Virtual Machines (VMs), powered by their…

3 days ago

Ubuntu Weekly Newsletter Issue 870

Welcome to the Ubuntu Weekly Newsletter, Issue 870 for the week of December 8 –…

4 days ago