{"id":1866,"date":"2020-11-07T10:22:56","date_gmt":"2020-11-07T10:22:56","guid":{"rendered":"https:\/\/linuxdigest.com\/?p=1866"},"modified":"2020-11-14T18:36:24","modified_gmt":"2020-11-14T18:36:24","slug":"using-the-cut-command-in-bash","status":"publish","type":"post","link":"https:\/\/linuxdigest.com\/howto\/using-the-cut-command-in-bash\/","title":{"rendered":"Using the cut command in bash"},"content":{"rendered":"\n

The cut command gives you the power to cut strings in the Linux shell or in bash scripts. Let’s look at some simple command examples.<\/p>\n\n\n\n

Cut strings from a file or from another command<\/h2>\n\n\n\n

You can either load the text you want from a file or pipe the string from another command. To load the text from a file specify the filename as the last argument of the command.<\/p>\n\n\n\n

~$ cut -f 1 file.txt \nLots of words in a file<\/code><\/pre>\n\n\n\n

You can also cut the output from a command using the | operator.<\/p>\n\n\n\n

$ echo \"Lots of words from a command\" | cut -f 1\nLots of words from a command<\/code><\/pre>\n\n\n\n

Cut by word<\/h2>\n\n\n\n

To get a particular word from a string you just need to specify a space as the delimiter using the -d argument. The -f argument specifies which word you want to get. The list of words starts from 1.<\/p>\n\n\n\n

~$ echo \"this is a multi word string\" | cut -d ' ' -f 2\nis<\/code><\/pre>\n\n\n\n

If you want to get more than one word, you can specify multiple words seperated by a comma.<\/p>\n\n\n\n

~$ echo \"this is a multi word string\" | cut -d ' ' -f 2,3,4\nis a multi<\/code><\/pre>\n\n\n\n

Cut by character position<\/h2>\n\n\n\n

To get the nth character from a string use the -c argument. In this example we want the fourth character from the string.<\/p>\n\n\n\n

~$ echo \"abcdefg\" | cut -c 4\nd<\/code><\/pre>\n\n\n\n

We can also specify a range of characters.<\/p>\n\n\n\n

~$ echo \"abcdefg\" | cut -c 4-6\ndef\n<\/code><\/pre>\n\n\n\n

Or a comma seperated list<\/p>\n\n\n\n

~$ echo \"abcdefg\" | cut -c 1,3,5\nace<\/code><\/pre>\n\n\n\n

I hope this short tutorial helps you to get started with the cut command. <\/p>\n","protected":false},"excerpt":{"rendered":"

The cut command gives you the power to cut strings in the Linux shell or in…<\/p>\n","protected":false},"author":1,"featured_media":1879,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"yoast_head":"\nUsing the cut command in bash - Linux Digest<\/title>\n<meta name=\"description\" content=\"Using the cut command in Linux, you can slice strings and get the parts you need. Either on the command line or in shell scripts.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/linuxdigest.com\/howto\/using-the-cut-command-in-bash\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using the cut command in bash - Linux Digest\" \/>\n<meta property=\"og:description\" content=\"Using the cut command in Linux, you can slice strings and get the parts you need. Either on the command line or in shell scripts.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxdigest.com\/howto\/using-the-cut-command-in-bash\/\" \/>\n<meta property=\"og:site_name\" content=\"Linux Digest\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-07T10:22:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-11-14T18:36:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxdigest.com\/wp-content\/uploads\/2020\/11\/scissors.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"683\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"The Linux Digest Guy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"The Linux Digest Guy\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/linuxdigest.com\/howto\/using-the-cut-command-in-bash\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/linuxdigest.com\/howto\/using-the-cut-command-in-bash\/\"},\"author\":{\"name\":\"The Linux Digest Guy\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2\"},\"headline\":\"Using the cut command in bash\",\"datePublished\":\"2020-11-07T10:22:56+00:00\",\"dateModified\":\"2020-11-14T18:36:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/linuxdigest.com\/howto\/using-the-cut-command-in-bash\/\"},\"wordCount\":200,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/linuxdigest.com\/#organization\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/linuxdigest.com\/howto\/using-the-cut-command-in-bash\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/linuxdigest.com\/howto\/using-the-cut-command-in-bash\/\",\"url\":\"https:\/\/linuxdigest.com\/howto\/using-the-cut-command-in-bash\/\",\"name\":\"Using the cut command in bash - Linux Digest\",\"isPartOf\":{\"@id\":\"https:\/\/linuxdigest.com\/#website\"},\"datePublished\":\"2020-11-07T10:22:56+00:00\",\"dateModified\":\"2020-11-14T18:36:24+00:00\",\"description\":\"Using the cut command in Linux, you can slice strings and get the parts you need. Either on the command line or in shell scripts.\",\"breadcrumb\":{\"@id\":\"https:\/\/linuxdigest.com\/howto\/using-the-cut-command-in-bash\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/linuxdigest.com\/howto\/using-the-cut-command-in-bash\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/linuxdigest.com\/howto\/using-the-cut-command-in-bash\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/linuxdigest.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using the cut command in bash\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/linuxdigest.com\/#website\",\"url\":\"https:\/\/linuxdigest.com\/\",\"name\":\"Linux Digest\",\"description\":\"Linux tutorials for everyone\",\"publisher\":{\"@id\":\"https:\/\/linuxdigest.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/linuxdigest.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/linuxdigest.com\/#organization\",\"name\":\"Linux Digest\",\"url\":\"https:\/\/linuxdigest.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png\",\"contentUrl\":\"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png\",\"width\":1102,\"height\":170,\"caption\":\"Linux Digest\"},\"image\":{\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2\",\"name\":\"The Linux Digest Guy\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/linuxdigest.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g\",\"caption\":\"The Linux Digest Guy\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using the cut command in bash - Linux Digest","description":"Using the cut command in Linux, you can slice strings and get the parts you need. Either on the command line or in shell scripts.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/linuxdigest.com\/howto\/using-the-cut-command-in-bash\/","og_locale":"en_US","og_type":"article","og_title":"Using the cut command in bash - Linux Digest","og_description":"Using the cut command in Linux, you can slice strings and get the parts you need. Either on the command line or in shell scripts.","og_url":"https:\/\/linuxdigest.com\/howto\/using-the-cut-command-in-bash\/","og_site_name":"Linux Digest","article_published_time":"2020-11-07T10:22:56+00:00","article_modified_time":"2020-11-14T18:36:24+00:00","og_image":[{"width":1024,"height":683,"url":"https:\/\/linuxdigest.com\/wp-content\/uploads\/2020\/11\/scissors.jpg","type":"image\/jpeg"}],"author":"The Linux Digest Guy","twitter_card":"summary_large_image","twitter_misc":{"Written by":"The Linux Digest Guy","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxdigest.com\/howto\/using-the-cut-command-in-bash\/#article","isPartOf":{"@id":"https:\/\/linuxdigest.com\/howto\/using-the-cut-command-in-bash\/"},"author":{"name":"The Linux Digest Guy","@id":"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2"},"headline":"Using the cut command in bash","datePublished":"2020-11-07T10:22:56+00:00","dateModified":"2020-11-14T18:36:24+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxdigest.com\/howto\/using-the-cut-command-in-bash\/"},"wordCount":200,"commentCount":0,"publisher":{"@id":"https:\/\/linuxdigest.com\/#organization"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxdigest.com\/howto\/using-the-cut-command-in-bash\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxdigest.com\/howto\/using-the-cut-command-in-bash\/","url":"https:\/\/linuxdigest.com\/howto\/using-the-cut-command-in-bash\/","name":"Using the cut command in bash - Linux Digest","isPartOf":{"@id":"https:\/\/linuxdigest.com\/#website"},"datePublished":"2020-11-07T10:22:56+00:00","dateModified":"2020-11-14T18:36:24+00:00","description":"Using the cut command in Linux, you can slice strings and get the parts you need. Either on the command line or in shell scripts.","breadcrumb":{"@id":"https:\/\/linuxdigest.com\/howto\/using-the-cut-command-in-bash\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxdigest.com\/howto\/using-the-cut-command-in-bash\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/linuxdigest.com\/howto\/using-the-cut-command-in-bash\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/linuxdigest.com\/"},{"@type":"ListItem","position":2,"name":"Using the cut command in bash"}]},{"@type":"WebSite","@id":"https:\/\/linuxdigest.com\/#website","url":"https:\/\/linuxdigest.com\/","name":"Linux Digest","description":"Linux tutorials for everyone","publisher":{"@id":"https:\/\/linuxdigest.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/linuxdigest.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/linuxdigest.com\/#organization","name":"Linux Digest","url":"https:\/\/linuxdigest.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/","url":"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png","contentUrl":"https:\/\/linuxdigest.com\/wp-content\/uploads\/2019\/11\/logo1.png","width":1102,"height":170,"caption":"Linux Digest"},"image":{"@id":"https:\/\/linuxdigest.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/linuxdigest.com\/#\/schema\/person\/29c97230aa94affab929a88c6a10adb2","name":"The Linux Digest Guy","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxdigest.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ac6bcf745dec6961360ccf2d2711f26c?s=96&d=mm&r=g","caption":"The Linux Digest Guy"}}]}},"_links":{"self":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts\/1866"}],"collection":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/comments?post=1866"}],"version-history":[{"count":2,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts\/1866\/revisions"}],"predecessor-version":[{"id":1868,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/posts\/1866\/revisions\/1868"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/media\/1879"}],"wp:attachment":[{"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/media?parent=1866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/categories?post=1866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxdigest.com\/wp-json\/wp\/v2\/tags?post=1866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}