{"id":6458,"date":"2018-08-28T21:37:54","date_gmt":"2018-08-28T17:37:54","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-cut-command\/"},"modified":"2018-08-28T21:37:54","modified_gmt":"2018-08-28T17:37:54","slug":"linux-cut-command-explained-for-beginners-with-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-cut-command-explained-for-beginners-with-examples\/","title":{"rendered":"Linux cut Command Explained for Beginners (with Examples)"},"content":{"rendered":"<p>In Linux, if you want to print a file&#8217;s content on stdout, the first command that comes to mind is <a href=\"https:\/\/www.howtoforge.com\/linux-cat-command\/\" target=\"_blank\" rel=\"noopener\">cat<\/a>. However, there may be times when the requirement is to remove certain part of the file and print only the rest of the content. You&#8217;ll be glad to know there exists a tool &#8211; dubbed <strong>cut<\/strong> &#8211; that lets you do this.<\/p>\n<p>In this article, we will discuss this tool using some easy to understand examples. But before we do that, it&#8217;s worth mentioning that all examples in this article have been tested on an Ubuntu 18.04 LTS machine.<\/p>\n<h2 id=\"linux-cut-command\">Linux cut command<\/h2>\n<p>The cut command in Linux lets users remove sections from each line of files. Following is its syntax:<\/p>\n<p class=\"command\">cut OPTION&#8230; [FILE]&#8230;<\/p>\n<p>Here&#8217;s what the man page says about this utility:<\/p>\n<pre readability=\"7\"> Print selected parts of lines from each FILE to standard output.<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 With no FILE, or when FILE is -, read standard input.<\/p><\/pre>\n<p>And following are some Q&amp;A-styled examples that should give you a good idea on how this utility works.<\/p>\n<h2 id=\"q-how-to-use-the-cut-command\">Q1. How to use the cut command?<\/h2>\n<p>The cut command expects user to provide a list of bytes, characters, or fields. You can provide bytes using the -b command line option.<\/p>\n<p>For example, suppose there&#8217;s a file named <em>file1.txt<\/em> that contains the following line:<\/p>\n<pre>abcdefghijklmnopqrstuvwxyz<\/pre>\n<p>And you want to only display the first three bytes. Then in this case, you can use the -b option in the following way:<\/p>\n<p class=\"command\">cut file1.txt -b1,2,3<\/p>\n<p>The output will be:<\/p>\n<pre>abc<\/pre>\n<p>You can also specify a range:<\/p>\n<p class=\"command\">cut file1.txt -b1-10<\/p>\n<p>Following is the output produced in this case:<\/p>\n<pre>abcdefghij<\/pre>\n<p>Moving on, you can also use hyphen (-) with a number to tell the cut command to either display all bytes after the byte at that number or all bytes before the byte at that number.<\/p>\n<p>For example, the following command will make sure that all bytes including and after the one at number 5 are displayed.<\/p>\n<p class=\"command\">cut file1.txt -b5-<\/p>\n<p>And the following command will display the first 5 bytes:<\/p>\n<p class=\"command\">cut file1.txt -b-5<\/p>\n<p><a class=\"fancybox\" id=\"img-cut-b-option\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/cut-b-option.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/08\/linux-cut-command-explained-for-beginners-with-examples.png\" alt=\"How to use the cut command\" width=\"360\" height=\"73\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-deal-with-characters\">Q2. How to deal with characters?<\/h2>\n<p>Sometimes, the file you pass to the cut command contains characters that are more than one byte in size. In that case, it&#8217;s advisable to use the -c option which lets the tool correctly understand which characters you want to display or remove.<\/p>\n<p>For example, \u2663 is a special\u00a0character that occupies multiple bytes. So if you want to use the cut command on a text stream that contains these kind of characters, then it&#8217;s better to use -c instead of -b. Functionality wise, both -c and -b work in similar way.<\/p>\n<h2 id=\"q-how-cut-works-with-delimiters\">Q3. How cut works with delimiters?<\/h2>\n<p>You can also make the cut command work with delimiters. For this, you can use the -d command line option.<\/p>\n<p>For example, suppose the input file contains comma-separated fields:<\/p>\n<p class=\"command\">Howtoforge, HTF, howtoforge.com<br \/>FaqForge, FF, faqforge.com<\/p>\n<p>And you want only first and third entries, then this can be done in the following way:<\/p>\n<p class=\"command\">cut file1.txt -d, -f1,3<\/p>\n<p>Note that the -f option lets you choose the fields you wanna display.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>So you see, the cut command has the potential to save a lot of your time if the task involves selective output of a file&#8217;s content. Here, in this tutorial, we have discussed some basic command line options this tool offers. To learn more, head to the tool&#8217;s <a href=\"https:\/\/linux.die.net\/man\/1\/cut\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a>.<\/p>\n<div class=\"authorbox\" readability=\"15\">\n<img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/08\/linux-cut-command-explained-for-beginners-with-examples.jpg\" alt=\"Himanshu Arora\" title=\"\"> <\/p>\n<p><strong>About Himanshu Arora<\/strong><\/p>\n<p>Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu&#8217;s work has also been featured in some of world&#8217;s other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.<\/p>\n<\/div>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Flinux-cut-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/08\/linux-cut-command-explained-for-beginners-with-examples-1.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-cut-command%2F&amp;text=Linux+cut+Command+Explained+for+Beginners+%28with+Examples%29&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/08\/linux-cut-command-explained-for-beginners-with-examples-2.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/08\/linux-cut-command-explained-for-beginners-with-examples-3.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-cut-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/08\/linux-cut-command-explained-for-beginners-with-examples-4.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In Linux, if you want to print a file&#8217;s content on stdout, the first command that comes to mind is cat. However, there may be times when the requirement is to remove certain part of the file and print only the rest of the content. You&#8217;ll be glad to know there exists a tool &#8211; &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-6458","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/6458","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=6458"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/6458\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=6458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=6458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=6458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}