{"id":5636,"date":"2021-04-12T18:36:32","date_gmt":"2021-04-12T18:36:32","guid":{"rendered":"https:\/\/linuxways.net\/?p=5636"},"modified":"2021-04-12T18:36:32","modified_gmt":"2021-04-12T18:36:32","slug":"use-cat-command-to-combine-text-files-in-ubuntu-20-04","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/use-cat-command-to-combine-text-files-in-ubuntu-20-04\/","title":{"rendered":"Use CAT Command to Combine Text Files in Ubuntu 20.04"},"content":{"rendered":"<p>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. In this article, we will cover the ways to combine text files using the CAT command in Ubuntu 20.04 system. We will also cover suitable examples to help our users better understand the proper usage of the CAT command.<\/p>\n<p>We will cover the following different scenarios that are helpful for our users to better understand the functionality of CAT command:<\/p>\n<ul>\n<li>Merge text from multiple text files.<\/li>\n<li>Merge text from multiple text files and save the output file in a sorted way to another file.<\/li>\n<li>Append text from one text file to another text file.<\/li>\n<li>Append text from the Terminal window directly into a text file.<\/li>\n<\/ul>\n<p>Let\u2019s cover all these in detail.<\/p>\n<h2>Merge text from multiple text files<\/h2>\n<p>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.<\/p>\n<p>Let\u2019s access the terminal window by using the <strong> Ctl+Alt+T <\/strong> shortcut. When the terminal window is opened, access the <strong>Desktop <\/strong>directory of the system. Once done, type the following command in the terminal window to display the text in the output:<\/p>\n<pre><strong>$ cat [textfile1.txt] [textfile2.txt]<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1058\" height=\"104\" class=\"wp-image-5637\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-48.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-48.png 1058w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-48-300x29.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-48-1024x101.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-48-768x75.png 768w\" sizes=\"auto, (max-width: 1058px) 100vw, 1058px\" \/><\/p>\n<p>To combine the output of the two files in the third file, type the following command:<\/p>\n<pre><strong>$ cat [textfile1.txt] [textfile2.txt] &gt; [textfile3.txt]<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"1322\" height=\"42\" class=\"wp-image-5638\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-49.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-49.png 1322w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-49-300x10.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-49-1024x33.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-49-768x24.png 768w\" sizes=\"auto, (max-width: 1322px) 100vw, 1322px\" \/><\/strong><\/p>\n<p>To print the output text file type:<\/p>\n<pre><strong>$ cat textfile3.txt<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1328\" height=\"140\" class=\"wp-image-5639\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-50.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-50.png 1328w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-50-300x32.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-50-1024x108.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-50-768x81.png 768w\" sizes=\"auto, (max-width: 1328px) 100vw, 1328px\" \/><\/p>\n<p>The text of the two files is now present in the third file. Here, I would like to mention 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.<\/p>\n<h2>Merge text from multiple text files and save the output file in a sorted way to another file<\/h2>\n<p>Now, let\u2019s 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\u2019s check the text in these files.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"820\" height=\"138\" class=\"wp-image-5640\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-51.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-51.png 820w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-51-300x50.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-51-768x129.png 768w\" sizes=\"auto, (max-width: 820px) 100vw, 820px\" \/><\/p>\n<p>We will create a new <strong>textfile3<\/strong> and combine the text of the two files i.e. textfile1 and textfile2 using:<\/p>\n<pre><strong>$ cat [textfile1.txt] [textfile2.txt] &gt; [textfile3.txt]<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1326\" height=\"36\" class=\"wp-image-5641\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-52.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-52.png 1326w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-52-300x8.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-52-1024x28.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-52-768x21.png 768w\" sizes=\"auto, (max-width: 1326px) 100vw, 1326px\" \/><\/p>\n<p>Combined output can be seen as displayed below using:<\/p>\n<pre><strong>$ cat textfile3.txt<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"860\" height=\"100\" class=\"wp-image-5642\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-53.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-53.png 860w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-53-300x35.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-53-768x89.png 768w\" sizes=\"auto, (max-width: 860px) 100vw, 860px\" \/><\/p>\n<p>To sort them out alphabetically, we will create another file <strong>textfile4.txt, <\/strong>and use the following command:<\/p>\n<pre><strong>$ cat textfile1.txt textfile2.txt textfile3.txt | sort &gt; textfile4.txt<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1372\" height=\"74\" class=\"wp-image-5643\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-54.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-54.png 1372w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-54-300x16.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-54-1024x55.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-54-768x41.png 768w\" sizes=\"auto, (max-width: 1372px) 100vw, 1372px\" \/><\/p>\n<p>On displaying the output you will see the sorted text values from the file.<\/p>\n<pre><strong>$ cat textfile4.txt<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"822\" height=\"102\" class=\"wp-image-5644\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-55.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-55.png 822w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-55-300x37.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-55-768x95.png 768w\" sizes=\"auto, (max-width: 822px) 100vw, 822px\" \/><\/p>\n<p>This way the text from the file can be sorted easily.<\/p>\n<h2>Append text from one text file to another text file<\/h2>\n<p>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.<\/p>\n<p>The first file or the source file is shown as follows:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1688\" height=\"548\" class=\"wp-image-5645\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-56.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-56.png 1688w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-56-300x97.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-56-1024x332.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-56-768x249.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-56-1536x499.png 1536w\" sizes=\"auto, (max-width: 1688px) 100vw, 1688px\" \/><\/p>\n<p>The destination file is:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1678\" height=\"404\" class=\"wp-image-5646\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-57.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-57.png 1678w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-57-300x72.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-57-1024x247.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-57-768x185.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-57-1536x370.png 1536w\" sizes=\"auto, (max-width: 1678px) 100vw, 1678px\" \/><\/p>\n<p>To append the text, use the following command:<\/p>\n<pre><strong>$ cat sourcetextfile.txt &gt;&gt; destinationfile.txt<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1106\" height=\"38\" class=\"wp-image-5647\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-58.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-58.png 1106w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-58-300x10.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-58-1024x35.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-58-768x26.png 768w\" sizes=\"auto, (max-width: 1106px) 100vw, 1106px\" \/><\/p>\n<p>The appended output file can be displayed using:<\/p>\n<pre><strong>$ cat textfile3.txt<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"830\" height=\"170\" class=\"wp-image-5648\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-59.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-59.png 830w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-59-300x61.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-59-768x157.png 768w\" sizes=\"auto, (max-width: 830px) 100vw, 830px\" \/><\/p>\n<h2>Append text from the Terminal window directly into a text file<\/h2>\n<p>To append text via the command line at the end of an already existing text file, use the following command:<\/p>\n<pre><strong>$ cat &gt;&gt; textfile.txt<\/strong><\/pre>\n<p>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.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1100\" height=\"102\" class=\"wp-image-5649\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-60.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-60.png 1100w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-60-300x28.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-60-1024x95.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-60-768x71.png 768w\" sizes=\"auto, (max-width: 1100px) 100vw, 1100px\" \/><\/p>\n<p>Once done, use the <strong>Ctl+D<\/strong> shortcut to save the changes. On accessing the file, you will see the text appended as displayed below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1682\" height=\"424\" class=\"wp-image-5650\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-61.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-61.png 1682w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-61-300x76.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-61-1024x258.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-61-768x194.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/04\/word-image-61-1536x387.png 1536w\" sizes=\"auto, (max-width: 1682px) 100vw, 1682px\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>In this article, we covered the detailed examples used for combining the text files in Ubuntu 20.04 system. 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.<\/p>","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>","protected":false},"author":1,"featured_media":5692,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[264,100],"class_list":["post-5636","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu","tag-cat-command","tag-ubuntu-20-04"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/5636","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=5636"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/5636\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/5692"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=5636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=5636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=5636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}