{"id":9051,"date":"2021-08-09T17:49:33","date_gmt":"2021-08-09T17:49:33","guid":{"rendered":"https:\/\/linuxways.net\/?p=9051"},"modified":"2021-08-09T17:49:33","modified_gmt":"2021-08-09T17:49:33","slug":"cut-command-in-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/cut-command-in-linux\/","title":{"rendered":"Cut command in Linux"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>Cut is a command used to extract parts of the line from files or piped data and export the result to standard output.<\/p>\n<p>it&#8217;s a built-in command-line utility in Linux and UNIX systems. The cut command can cut parts of the line by byte position, field, and character.<\/p>\n<p>Below is the guide on how to use the cut command in Linux.<\/p>\n<h2>The syntax of cut command<\/h2>\n<pre>$ cut OPTION\u2026 [FILE]...<\/pre>\n<p><strong>[options]<\/strong><\/p>\n<p><strong>-b (byte) <\/strong>Slicing by bytes<\/p>\n<p><strong>-f (field) <\/strong>Slicing by fields<\/p>\n<p><strong>-c (character)<\/strong> Slicing by characters<\/p>\n<p><strong>-d (delimiter) <\/strong> Slicing by delimiter instead of <strong>TAB<\/strong> delimiter<\/p>\n<p><strong>&#8211;complement <\/strong>Complement the output<\/p>\n<p><strong>&#8211;output-delimiter <\/strong> Change the output delimiter<\/p>\n<h2>How to use cut command<\/h2>\n<p><strong>1. -b (byte) <\/strong><\/p>\n<p>To cut out specific bytes. The range of bytes can be specified with \u201c-\u201d. Tabs and backspaces are also considered 1 byte.<\/p>\n<pre>$ cut -b [file]<\/pre>\n<p>For example, in hello.txt file contains the text \u201c<strong>Hello World!<\/strong>\u201d. Now we will cut out specific bytes:<\/p>\n<pre>$ cut -b 1 hello.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"939\" height=\"71\" class=\"wp-image-9052\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-100.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-100.png 939w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-100-300x23.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-100-768x58.png 768w\" sizes=\"auto, (max-width: 939px) 100vw, 939px\" \/><\/p>\n<p>In the above command, they output the first character as &#8220;<strong>H<\/strong>&#8221;<\/p>\n<pre>$ cut -b 1,6,7 hello.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"939\" height=\"66\" class=\"wp-image-9053\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-101.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-101.png 939w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-101-300x21.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-101-768x54.png 768w\" sizes=\"auto, (max-width: 939px) 100vw, 939px\" \/><\/p>\n<p>In the above command, the output 1st, 6th, and 7th characters. The 6th character is an empty character.<\/p>\n<p>List with ranges:<\/p>\n<pre>$ cut -b 1-3,7-9 hello.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"72\" class=\"wp-image-9054\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-102.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-102.png 940w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-102-300x23.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-102-768x59.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/p>\n<p>It also selects bytes from beginning up to the end of the line through a special form:<\/p>\n<pre>$ cut -b 1- hello.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"66\" class=\"wp-image-9055\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-103.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-103.png 940w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-103-300x21.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-103-768x54.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/p>\n<p>In the above command, the output from the 1st byte to the last byte.<\/p>\n<pre>$ cut -b -4 hello.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"65\" class=\"wp-image-9056\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-104.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-104.png 940w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-104-300x21.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-104-768x53.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/p>\n<p>In the above command, the output from the 1st byte to the 4th byte.<\/p>\n<p><strong>2. -f (field)<\/strong><\/p>\n<p>To extract specific fields. When no delimiter is specified, the default delimiter will be <strong>TAB<\/strong>.<\/p>\n<pre>$ cut -f [FILE]<\/pre>\n<p>For example, here we have field.txt file:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"73\" class=\"wp-image-9057\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-105.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-105.png 940w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-105-300x23.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-105-768x60.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/p>\n<p>Now we will extract the 1st and the 2nd field:<\/p>\n<pre>$ cut -f 1,2 field.txt<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"86\" class=\"wp-image-9058\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-106.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-106.png 940w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-106-300x27.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-106-768x70.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/p>\n<p><strong>3. -c (character)<\/strong><\/p>\n<p>To cut by character. This can be a list separated by \u201c<strong>.<\/strong>\u201d, \u201c<strong>,<\/strong>\u201d, \u201c<strong>;<\/strong>\u201d; \u201c<strong>&#8211;<\/strong>\u201d. Tabs and backspaces are also considered a character.<\/p>\n<pre>$ cut -c [FILE]<\/pre>\n<p>For example, here we have character.txt file:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"55\" class=\"wp-image-9059\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-107.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-107.png 940w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-107-300x18.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-107-768x45.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/p>\n<p>Now we will extract the 2nd, 4th, 6th character:<\/p>\n<pre>$ cut -c 2,4,6 character.txt<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"63\" class=\"wp-image-9060\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-108.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-108.png 940w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-108-300x20.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-108-768x51.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/p>\n<p><strong>4. -d (delimiter)<\/strong><\/p>\n<p>To cut out specific delimiters. This option is often used with <strong>-f (field)<\/strong>.<\/p>\n<p>For example, here we have dlmt.txt file separated by \u201c<strong>:<\/strong>\u201d<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"941\" height=\"69\" class=\"wp-image-9061\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-109.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-109.png 941w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-109-300x22.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-109-768x56.png 768w\" sizes=\"auto, (max-width: 941px) 100vw, 941px\" \/><\/p>\n<p>Now we will extract from the 1st to the 3rd field:<\/p>\n<pre>$ cut -d \":\" -f 1-3 dlmt.txt<\/pre>\n<p>Output: <img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"85\" class=\"wp-image-9062\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-110.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-110.png 940w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-110-300x27.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-110-768x69.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/p>\n<p><strong>5. &#8211;complement<\/strong><\/p>\n<p>To print all fields except those that are not selected with <strong>-f<\/strong> option.<\/p>\n<p>For example, here we have field.txt file:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"73\" class=\"wp-image-9063\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-111.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-111.png 940w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-111-300x23.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-111-768x60.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/p>\n<p>Now we will extract all fields except the 1st and 4th fields:<\/p>\n<pre>$ cut --complement -f 1,4 field.txt<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"92\" class=\"wp-image-9064\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-112.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-112.png 940w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-112-300x29.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-112-768x75.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/p>\n<p><strong>6. &#8211;output-delimiter<\/strong><\/p>\n<p>To change the output delimiter. <strong> &#8211;output-delimiter=\u201ddelimiter\u201d<\/strong>.<\/p>\n<p>Now we will extract all fields except the 1st and 4th field but the output is separated by \u201c<strong>?<\/strong>\u201d :<\/p>\n<p>&amp; cut &#8211;complement -f 1,4 field.txt &#8211;output-delimiter=&#8221;?&#8221;<\/p>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"88\" class=\"wp-image-9065\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-113.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-113.png 940w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-113-300x28.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-113-768x72.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>You\u2019ve already gone through the details of how to use the cut command in Linux.<\/p>\n<p>Thanks for reading.<\/p>","protected":false},"excerpt":{"rendered":"<p>Introduction Cut is a command used to extract parts of the line from files or piped data and export the result to standard output. it&#8217;s a built-in command-line&hellip;<\/p>","protected":false},"author":1,"featured_media":9169,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,5,83,165,2],"tags":[469],"class_list":["post-9051","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-debian","category-mint","category-opensuse","category-red-hat","category-ubuntu","tag-cut-command"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/9051","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=9051"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/9051\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/9169"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=9051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=9051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=9051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}