{"id":402192,"date":"2022-02-01T16:00:05","date_gmt":"2022-02-01T13:00:05","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-use-the-linux-cut-command\/"},"modified":"2022-02-01T16:00:05","modified_gmt":"2022-02-01T13:00:05","slug":"how-to-use-the-linux-cut-command","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-linux-cut-command\/","title":{"rendered":"#How to Use the Linux cut Command"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2d7e099a7a1\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2d7e099a7a1\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-linux-cut-command\/#The_cut_Command\" >The cut Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-linux-cut-command\/#First_Steps_With_cut\" >First Steps With cut<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-linux-cut-command\/#Using_cut_With_Characters\" >Using cut With Characters<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-linux-cut-command\/#Using_cut_With_Delimited_Data\" >Using cut With Delimited Data<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-linux-cut-command\/#Piping_cut_Into_cut\" >Piping cut Into cut<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-linux-cut-command\/#The_Output_Delimiter\" >The Output Delimiter<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-linux-cut-command\/#An_Oldie_but_Goldie\" >An Oldie but Goldie<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Use the Linux cut Command&#8221;<\/strong><\/p>\n<div>\n<figure style=\"width: 1200px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage size-full wp-image-770699 \" data-pagespeed-lazy-srcset=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/shutterstock_1278851809.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/shutterstock_1278851809.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/shutterstock_1278851809.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"A terminal display on an open laptop screen\" width=\"1200\" height=\"675\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><figcaption class=\"wp-caption-text\"><span class=\"type:primaryImage imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-vector\/command-line-interface-cli-programming-language-1278851809\">fatmawati achmad zaenuri\/Shutterstock.com<\/a><\/span><\/figcaption><\/figure>\n<p>The Linux <code>cut<\/code> command lets you extract portions of text from files or data streams. It\u2019s especially useful for working with delimited data, such as CSV files. Here\u2019s what you need to know.<\/p>\n<h2 id=\"the-cut-command\"><span class=\"ez-toc-section\" id=\"The_cut_Command\"><\/span>The cut Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>cut<\/code> command is a veteran of the Unix world, making its debut in 1982 as part of AT&amp;T System III UNIX. Its purpose in life is to snip out sections of text from files or streams, according to the criteria that you set. Its syntax is as simple as its purpose, but it is this joint simplicity that makes it so useful.<\/p>\n<p>In the time-honored UNIX way, by combining <code>cut<\/code> with other utilities such as <code>grep<\/code> you can create elegant and powerful solutions to challenging problems. While there are different versions of <code>cut<\/code>, we\u2019re going to discuss the standard GNU\/Linux version. Be aware that other versions, notably the <code>cut<\/code> found in BSD variants, don\u2019t include all the options described here.<\/p>\n<p>You can check which version is installed on your computer by issuing this command:<\/p>\n<pre>cut --version<\/pre>\n<p>If you see \u201cGNU coreutils\u201d in the output you\u2019re on the version we\u2019re going to describe in this article. All versions of <code>cut<\/code> have some of this functionality, but the Linux version has had enhancements added to it.<\/p>\n<h2 id=\"first-steps-with-cut\"><span class=\"ez-toc-section\" id=\"First_Steps_With_cut\"><\/span>First Steps With cut<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Whether we\u2019re piping information into <code>cut<\/code> or using <code>cut<\/code> to read a file, the commands we use are the same. Anything you can do to a stream of input with <code>cut<\/code> can be done on a line of text from a file, and\u00a0<em>vice versa<\/em>. We can tell <code>cut<\/code> to work with bytes, characters, or delimited fields.<\/p>\n<p>To select a single byte, we use the <code>-b<\/code> (byte) option and tell <code>cut<\/code> which byte or bytes we want. In this case, it is byte five. We\u2019re sending the string \u201chow-to geek\u201d into the <code>cut<\/code> command with a pipe, \u201c|\u201d, from <code>echo<\/code> .<\/p>\n<pre>echo 'how-to geek' | cut -b 5<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-775827\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/12\/1-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Extracting a single byte with cut\" width=\"644\" height=\"95\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The fifth byte in that string is \u201ct\u201d, so <code>cut<\/code> responds by printing \u201ct\u201d in the terminal window.<\/p>\n<p>To specify a\u00a0<em>range<\/em>\u00a0we use a hyphen. To extract bytes 5 through to\u2014and including\u201411, we\u2019d issue this command:<\/p>\n<pre>echo 'how-to geek' | cut -b 5-11<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-775828\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/12\/2-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Extracting a range of bytes with cut\" width=\"644\" height=\"95\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You can supply multiple single bytes or ranges by separating them with commas. To extract byte 5 and byte 11, use this command:<\/p>\n<pre>echo 'how-to geek' | cut -b 5,11<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-775829\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/12\/3-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Extracting two bytes with cut\" width=\"644\" height=\"95\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>To get the first letter of each word we can use this command:<\/p>\n<pre>echo 'how-to geek' | cut -b 1,5,8<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-775830\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/12\/4-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Extracting three bytes with cut\" width=\"644\" height=\"95\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>If you use the hyphen without a\u00a0<em>first<\/em>\u00a0number, <code>cut<\/code> returns everything from position 1 up to the number. If you use the hyphen without a\u00a0<em>second<\/em>\u00a0number, <code>cut<\/code> returns everything from the first number to the end of the stream or line.<\/p>\n<pre>echo 'how-to geek' | cut -b -6<\/pre>\n<pre>echo 'how-to geek' | cut -b 8-<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-775831\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/12\/4-5.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Extracting ranges of bytes with cut\" width=\"644\" height=\"130\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 id=\"using-cut-with-characters\"><span class=\"ez-toc-section\" id=\"Using_cut_With_Characters\"><\/span>Using cut With Characters<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Using <code>cut<\/code> with characters is pretty much the same as using it with bytes. In both cases, special care must be taken with complex characters. By using the <code>-c<\/code> (character) option, we tell <code>cut<\/code> to work in terms of characters, not bytes.<\/p>\n<pre>echo 'how-to geek' | cut -c 1,5,8<\/pre>\n<pre>echo 'how-to geek' | cut -c 8-11<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-775832\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/12\/5-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Extracting characters and ranges of characters with cut\" width=\"644\" height=\"130\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>These work exactly as you\u2019d expect. But take a look at this example. It\u2019s a six-letter word, so asking <code>cut<\/code> to return the characters from one to six should return the entire word. But it doesn\u2019t. It\u2019s one character short. To see the whole word we have to ask for the characters from one to seven.<\/p>\n<pre>echo 'pi\u00f1ata' | cut -c 1-6<\/pre>\n<pre>echo 'pi\u00f1ata' | cut -c 1-7<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-775833\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/12\/6-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Special characters can take up more than one character\" width=\"644\" height=\"130\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The issue is the character \u201c\u00f1\u201d is actually made up out of two bytes. We can see this quite easily. We\u2019ve got a short text file containing this line of text:<\/p>\n<pre>cat unicode.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-775834\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/12\/7-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The contents of the short text file\" width=\"644\" height=\"95\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>We\u2019ll examine that file with the <code>hexdump<\/code> utility. Using the <code>-C<\/code> (canonical) option gives us a table of hexadecimal digits with the ASCII equivalent on the right. In the ASCII table, the \u201c\u00f1\u201d isn\u2019t shown, instead, there are dots representing <em>two<\/em> non-printable characters. These are the bytes highlighted in the hexadecimal table.<\/p>\n<pre>hexdump -C unicode.txt<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-775835\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/12\/8-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Hexdump of the test text file\" width=\"644\" height=\"145\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>These two bytes are used by the displaying program\u2014in this case, the Bash shell\u2014to identify the \u201c\u00f1.\u201d Many Unicode characters use three or more bytes to represent a single character.<\/p>\n<p>If we ask for character 3 or character 4 we\u2019re shown the symbol for a non-printing character. If we ask for bytes 3 <em>and<\/em> 4, the shell interprets them as \u201c\u00f1.\u201d<\/p>\n<pre>echo 'pi\u00f1ata' | cut -c 3<\/pre>\n<pre>echo 'pi\u00f1ata' | cut -c 4<\/pre>\n<pre>echo 'pi\u00f1ata' | cut -c 3-4<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-775836\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/12\/9-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using cut to extract the characters that make up a special character\" width=\"644\" height=\"170\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 id=\"using-cut-with-delimited-data\"><span class=\"ez-toc-section\" id=\"Using_cut_With_Delimited_Data\"><\/span>Using cut With Delimited Data<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We can ask <code>cut<\/code> to split lines of text using a specified delimiter. By default, cut uses a tab character but it is easy to tell it to use whatever we want. The fields in the \u201c\/etc\/passwd\u201d file are separated by colons \u201c:\u201d, so we\u2019ll use that as our delimiter and extract some text.<\/p>\n<p>The portions of text between the delimiters are called\u00a0<em>fields<\/em>, and are referenced just like bytes or characters, but they\u2019re preceded by the <code>-f<\/code> (fields) option. You can leave a space between the \u201cf\u201d and the digit, or not.<\/p>\n<p>The first command uses the <code>-d<\/code> (delimiter) option to tell cut to use \u201c:\u201d as the delimiter. It\u2019s going to pull the first field out of each line in the \u201c\/etc\/passwd\u201d file. That\u2019ll be a long list so we\u2019re using <code>head<\/code> with the <code>-n<\/code> (number) option to show the first five responses only. The second command does the same thing but uses <code>tail<\/code> to show us the last five responses.<\/p>\n<pre>cut -d':' -f1 \/etc\/passwd | head -n 5<\/pre>\n<pre>cut -d':' -f2 \/etc\/passwd | tail -n 5<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-775839\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/12\/11-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Extracting a range of fields from the \/etc\/passwd file\" width=\"644\" height=\"280\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>To extract a selection of fields, list them as a comma-separated list. This command will extract fields one to three, five, and six.<\/p>\n<pre>cut -d':' -f1-3,5,6 \/etc\/passwd | tail -n 5<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-775838\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/12\/12-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Extracting a range of fields from the \/etc\/passwd file\" width=\"644\" height=\"170\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>By including <code>grep<\/code> in the command, we can look for lines that include \u201c\/bin\/bash.\u201d The means we can list only those entries that have Bash as their default shell. That\u2019ll usually be the \u201cnormal\u201d user accounts. We\u2019ll ask for fields from one to six because the seventh field is the default shell field and we already know what that is\u2014we\u2019re searching for it.<\/p>\n<pre>grep \"\/bin\/bash\" \/etc\/passwd | cut -d':' -f1-6<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-775840\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/12\/13-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Extracting fields one to six from the \/etc\/passwd file\" width=\"644\" height=\"130\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Another way to include all fields apart from one is to use the <code>--complement<\/code> option. This inverts the field selection and shows everything that\u00a0<em>hasn\u2019t<\/em>\u00a0been requested. Let\u2019s repeat the last command but only ask for field seven. Then we\u2019ll run that command again with the <code>--complement<\/code> option.<\/p>\n<pre>grep \"\/bin\/bash\" \/etc\/passwd | cut -d':' -f7<\/pre>\n<pre>grep \"\/bin\/bash\" \/etc\/passwd | cut -d':' -f7 --complement<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-775841\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/12\/14-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using the --complement option to invert a field selection\" width=\"644\" height=\"175\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The first command finds a list of entries, but field seven gives us nothing to distinguish between them, so we don\u2019t know who the entries refer to. In the second command, by adding the <code>--complement<\/code> option we get everything except field seven.<\/p>\n<h2 id=\"piping-cut-into-cut\"><span class=\"ez-toc-section\" id=\"Piping_cut_Into_cut\"><\/span>Piping cut Into cut<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Sticking with the \u201c\/etc\/passwd\u201d file, let\u2019s extract field five. This is the actual name of the user who owns the user account.<\/p>\n<pre>grep \"\/bin\/bash\" \/etc\/passwd | cut -d':' -f5<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-775845\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/12\/15-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The fifth field from the \/etc\/passwd file can have comma separated subfields\" width=\"644\" height=\"130\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The fifth field has subfields separated by commas. They\u2019re rarely populated so they show up as a line of commas.<\/p>\n<p>We can remove the commas by piping the output of the previous command into another invocation of <code>cut<\/code> . The second instance of <code>cut<\/code>\u00a0uses the comma \u201c,\u201d as its delimiter. The <code>-s<\/code> (only delimited) option tells <code>cut<\/code> to suppress results that don\u2019t have the delimiter in them at all.<\/p>\n<pre>grep \"\/bin\/bash\" \/etc\/passwd | cut -d':' -s -f5 | cut -d',' -s -f1<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-775847\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/12\/16-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Piping cut into cut to deal with two types of delimiter\" width=\"644\" height=\"123\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Because the root entry doesn\u2019t have comma subfields in the fifth field it is suppressed, and we get the results we\u2019re after\u2014a list of the names of the \u201creal\u201d users configured on this computer.<\/p>\n<p><strong>RELATED:<\/strong> <strong><em>How Do Linux File Permissions Work?<\/em><\/strong><\/p>\n<h2 id=\"the-output-delimiter\"><span class=\"ez-toc-section\" id=\"The_Output_Delimiter\"><\/span>The Output Delimiter<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We\u2019ve got a small file with some Comma Separated Values in them. The fields in this dummy data are:<\/p>\n<ul>\n<li><strong>ID<\/strong>: A database ID number<\/li>\n<li><strong>First<\/strong>: The first name of the subject.<\/li>\n<li><strong>Last<\/strong>: The last name of the subject.<\/li>\n<li><strong>email<\/strong>: Their email address.<\/li>\n<li><strong>IP Address<\/strong>: Their IP Address.<\/li>\n<li><strong>Brand<\/strong>: The brand of motor vehicle they drive.<\/li>\n<li><strong>Model<\/strong>: The model of motor vehicle they drive.<\/li>\n<li><strong>Year<\/strong>: The year their motor vehicle was built.<\/li>\n<\/ul>\n<pre>cat small.csv<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-775846\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/12\/10-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"A text file of dummy CSV data\" width=\"644\" height=\"166\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>If we tell cut to use the comma as the delimiter we can extract fields just like we did before. Sometimes you\u2019ll have a requirement to extract data from a file, but you don\u2019t want to have the field delimiter included in the results. Using the <code>--output-delimiter<\/code> we can tell cut what character\u2014or in fact, character\u00a0<em>sequence<\/em>\u2014to use instead of the actual delimiter.<\/p>\n<pre>cut -d ',' -f 2,3 small.csv<\/pre>\n<pre>cut -d ',' -f 2,3 small.csv --output-delimiter=\" \"<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-775848\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/12\/17-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using the --output-delimiter to change the delimiter in the results\" width=\"644\" height=\"280\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The second command tells <code>cut<\/code> to replace the commas with spaces.<\/p>\n<p>We can take this further and use this feature to convert the output to a vertical list. This command uses a new line character as the output delimiter. Note the \u201c$\u201d that we need to include to have the newline character acted upon, and not interpreted as a literal sequence of two characters.<\/p>\n<p>We\u2019ll use <code>grep<\/code> to filter out the entry for Morgana Renwick, and ask <code>cut<\/code> to print all fields from field two to the end of the record, and to use a newline character as the output delimiter.<\/p>\n<pre>grep 'renwick' small.csv | cut -d ',' -f2- --output-delimiter=$''<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-775849\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/12\/18-1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Converting a record to a list by using a newline character as the output delimiter\" width=\"644\" height=\"201\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 id=\"an-oldie-but-goldie\"><span class=\"ez-toc-section\" id=\"An_Oldie_but_Goldie\"><\/span>An Oldie but Goldie<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>At the time of writing, the little cut command is <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>roaching its 40th birthday, and we\u2019re still using it and writing about it today. I suppose cutting up text today is the same as it was 40 years ago. That is, a lot easier when you have the right tool to hand.<\/p>\n<p><strong>RELATED:<\/strong> <strong><em>37 Important Linux Commands You Should Know<\/em><\/strong><\/p>\n<\/div>\n<p><script>\n setTimeout(function(){\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s) } (window, document,'script',\n  'https:\/\/connect.facebook.net\/en_US\/fbevents.js');\n   fbq('init', '335401813750447');\n   fbq('track', 'PageView');\n  },3000);\n<\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.howtogeek.com\/775824\/how-to-use-the-linux-cut-command\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Use the Linux cut Command&#8221; fatmawati achmad zaenuri\/Shutterstock.com The Linux cut command lets you extract portions of text from files or data streams. It\u2019s especially useful for working with delimited data, such as CSV files. Here\u2019s what you need to know. The cut Command The cut command is a veteran of the Unix&#8230;<\/p>\n","protected":false},"author":1,"featured_media":402193,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/shutterstock_1278851809.png?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-402192","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/402192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=402192"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/402192\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/402193"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=402192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=402192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=402192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}