{"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
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\nYou 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\nCut 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\nIf 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\nCut 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\nWe 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\nOr a comma seperated list<\/p>\n\n\n\n
~$ echo \"abcdefg\" | cut -c 1,3,5\nace<\/code><\/pre>\n\n\n\nI 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":"\n
Using the cut command in bash - Linux Digest<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n\n\n\n\t\n\t\n\t\n