{"id":15250,"date":"2022-02-15T18:10:19","date_gmt":"2022-02-15T18:10:19","guid":{"rendered":"https:\/\/linuxways.net\/?p=15250"},"modified":"2022-02-15T18:10:19","modified_gmt":"2022-02-15T18:10:19","slug":"how-would-you-combine-2-text-files-with-the-cat-command","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/how-would-you-combine-2-text-files-with-the-cat-command\/","title":{"rendered":"How Would You Combine 2 Text Files With the Cat Command"},"content":{"rendered":"<p>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 \u201ccat\u2019 command on Ubuntu 20.04(Linux Operating System).<\/p>\n<p>As the name suggests, The &#8220;cat&#8221; command is abbreviated as &#8220;concatenate&#8221; 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.<\/p>\n<h2>Combine 2 Text Files with the Cat Command<\/h2>\n<p>Below mentioned are the ways to organize the two files into one using the cat command which are discussed in detail below:<\/p>\n<ul>\n<li>Combine the two files into one file using the cat command alphabetically.<\/li>\n<li>Combine the two files into one file using the cat command numerically.<\/li>\n<\/ul>\n<p><strong>Syntax:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>\n<pre><strong>$ cat [options] filename<\/strong><\/pre>\n<\/th>\n<\/tr>\n<\/thead>\n<\/table>\n<p>Options will help in formatting the display content of the file.<\/p>\n<table>\n<thead>\n<tr>\n<th><strong>Options <\/strong><\/th>\n<th><strong>Explanation<\/strong><\/th>\n<\/tr>\n<tr>\n<th>-A<\/th>\n<th>Equal to -vET<\/th>\n<\/tr>\n<tr>\n<th>-b<\/th>\n<th>Display all non-empty output lines with numbering<\/th>\n<\/tr>\n<tr>\n<th>-e<\/th>\n<th>Equal to -vE<\/th>\n<\/tr>\n<tr>\n<th>-E<\/th>\n<th>Put $ at the end of each output line<\/th>\n<\/tr>\n<tr>\n<th>-n<\/th>\n<th>Display all output lines with numbering<\/th>\n<\/tr>\n<tr>\n<th>-s<\/th>\n<th>Repeated empty output lines are suppressed.<\/th>\n<\/tr>\n<tr>\n<th>-t<\/th>\n<th>Equal to -vT.<\/th>\n<\/tr>\n<\/thead>\n<\/table>\n<p>The &#8220;cat&#8221; command mostly comes pre-installed in new Ubuntu versions, but if it is not installed run the below-mentioned command to install it:<\/p>\n<table>\n<thead>\n<tr>\n<th>\n<pre><strong>$ sudo apt install coreutils<\/strong><\/pre>\n<\/th>\n<\/tr>\n<\/thead>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1075\" height=\"371\" class=\"wp-image-15251\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-441.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-441.png 1075w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-441-300x104.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-441-1024x353.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-441-768x265.png 768w\" sizes=\"auto, (max-width: 1075px) 100vw, 1075px\" \/><\/p>\n<h2><strong>Combine the two files into one file using the cat command alphabetically<\/strong><\/h2>\n<p>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 &#8220;linux1.txt&#8221; and \u201clinux2.txt&#8221; and sort the data alphabetically in file &#8220;alpha_linux.txt&#8221;.<\/p>\n<table>\n<thead>\n<tr>\n<th>\n<pre><strong>$ cat linux1.txt linux2.txt | sort &gt; alpha_linux.txt<\/strong><\/pre>\n<\/th>\n<\/tr>\n<\/thead>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1282\" height=\"158\" class=\"wp-image-15252\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-442.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-442.png 1282w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-442-300x37.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-442-1024x126.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-442-768x95.png 768w\" sizes=\"auto, (max-width: 1282px) 100vw, 1282px\" \/><\/p>\n<p>Standard Redirect Symbol (&gt;) is used before the filename to insert content into a file, but it will overwrite the file if some content existed previously. Use &#8220;&gt;&gt;&#8221; to avoid the overwrite. Sort is a command to organize data according to the mentioned standard.<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>Below is the output of the above command.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1300\" height=\"595\" class=\"wp-image-15253\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-443.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-443.png 1300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-443-300x137.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-443-1024x469.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-443-768x352.png 768w\" sizes=\"auto, (max-width: 1300px) 100vw, 1300px\" \/><\/p>\n<h2><strong>Combine the two files into one file using the cat command numerically<\/strong><\/h2>\n<p>To combine the two files into one numerically, use the &#8220;-n&#8221; option with the sort and cat command. This option is only beneficial if your file&#8217;s lines begin with line numbers. Please remember that &#8220;03&#8221; would be less than &#8220;2&#8221; in the default manner. The below-mentioned command will merge and sort &#8220;linux1.txt&#8221; and &#8220;linux2.txt&#8221; numerically in ascending order of line numbers into the file &#8220;num_linux.txt&#8221;.<\/p>\n<table>\n<thead>\n<tr>\n<th>\n<pre><strong>$ cat linux1.txt linux2.txt | sort \u2013n &gt; num_linux.txt<\/strong><\/pre>\n<\/th>\n<\/tr>\n<\/thead>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1307\" height=\"227\" class=\"wp-image-15254\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-444.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-444.png 1307w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-444-300x52.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-444-1024x178.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-444-768x133.png 768w\" sizes=\"auto, (max-width: 1307px) 100vw, 1307px\" \/><\/p>\n<p>The standard redirect symbol (&gt;) is used before the filename to insert content into the file, but it will overwrite the file if some content already exists. Use &#8220;&gt;&gt;&#8221; to avoid the overwrite. Sort is a command to organize data according to the mentioned standard.<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>Below is the output of the above command.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1290\" height=\"490\" class=\"wp-image-15255\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-445.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-445.png 1290w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-445-300x114.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-445-1024x389.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-445-768x292.png 768w\" sizes=\"auto, (max-width: 1290px) 100vw, 1290px\" \/><\/p>\n<p>The below-mentioned command will merge and sort &#8220;linux1.txt&#8221; and &#8220;linux2.txt&#8221; numerically in reverse order of line numbers into the file &#8220;num_linux.txt&#8221;.<\/p>\n<table>\n<thead>\n<tr>\n<th>\n<pre><strong>$ cat linux1.txt linux2.txt | sort \u2013nr &gt; num_linux.txt<\/strong><\/pre>\n<\/th>\n<\/tr>\n<\/thead>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1313\" height=\"170\" class=\"wp-image-15256\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-446.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-446.png 1313w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-446-300x39.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-446-1024x133.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-446-768x99.png 768w\" sizes=\"auto, (max-width: 1313px) 100vw, 1313px\" \/><\/p>\n<p><strong>-nr will perform sorting in reverse order (descending order).<\/strong><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>Below is the output of the above command.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1326\" height=\"542\" class=\"wp-image-15257\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-447.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-447.png 1326w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-447-300x123.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-447-1024x419.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/02\/word-image-447-768x314.png 768w\" sizes=\"auto, (max-width: 1326px) 100vw, 1326px\" \/><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>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.<\/p>","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>","protected":false},"author":1,"featured_media":15289,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,891,5,83,165,2],"tags":[264],"class_list":["post-15250","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-debian","category-manjaro","category-mint","category-opensuse","category-red-hat","category-ubuntu","tag-cat-command"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/15250","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=15250"}],"version-history":[{"count":1,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/15250\/revisions"}],"predecessor-version":[{"id":15269,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/15250\/revisions\/15269"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/15289"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=15250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=15250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=15250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}