Friday, November 8, 2024
HomeA-Z Commands50 Simple and Useful Linux Cut Command in Unix with Examples

50 Simple and Useful Linux Cut Command in Unix with Examples

The cut command is used for cutting out sections of the standard input stream or data files utilizing the Unix cut utility. It is part of the GNU Coreutils package and the BSD Base System, hence, available on every Linux and BSD systems by default. The cut command in Unix allows cutting of sections based on byte positions, characters, or fields separated by a delimiter like the ‘-‘ or ‘:’ characters. Our guide provides a practical introduction to the Linux cut command using a well-curated set of examples. Try them out alongside reading this post for gaining first-hand experience.

Examples of Linux Cut Command in Unix


Our experts have tried their best to make this guide friendly to new Linux users. Additionally, it will serve as a handy reference point for seasoned users alike. We encourage readers to try out the commands as they explore them. We’ll demonstrate these Linux Cut commands using both the standard input and a reference file. You can copy-paste the contents of the file from here and create it in your system.

Reference File Used for Demonstration Purposes


We’re using a text file called test.txt residing in the home directory. The file contains five rows or lines all containing four columns. Each row includes the name of a country, her capital, currency, and population; all separated by the delimiter colon. Create this file in your system and fill that up with the contents below.

Linux cut command examples

France: Paris: Euro: 65 million
Austria: Vienna: Euro: 8 million
Turkey: Ankara: Lira: 79 million
Belgium: Brussels: Euro: 11 million
Japan: Tokyo: Yen: 126 million

Syntax of the Cut Command in Unix


The Linux cut command has the below syntax.

cut OPTION... [FILE]...

The OPTIONs include b for (byte-based cutting), f (field), c (character), d (delimiter), complement, and –output-delimiter. FILE is the filename. We’ll also show how cut works with the standard input stream.

To cut text from the input stream, we’ll use the echo command and pipe (|) its output to the cut command. The same method can be used for providing cut’s input from cat.

Cut Text Based on Byte Positions


The b option provided by the cut utility allows us to cut sections of a text-based on their byte positions. We need to use the cut command with the flag -b followed by the byte numbers for this purpose.

1. Cut Only a Single Byte from the Input Stream

$ echo "cutting text from input" | cut -b 1

The above command echoes the string “cutting text from input” to the standard output and pipes it as an input to the cut command. The cut command will cut just the first byte(c) from this string as only 1 was provided with the -b flag.

2. Cut Specific Bytes from the Input Stream

$ echo "cutting text from input" | cut -b 1,3

This command will only cut the first and third byte of the string “cutting text from input” and will display “ct” as its output. Try it out with some different byte positions.

3. Cut Range of Bytes from the Input Stream

$ echo "cutting text from input" | cut -b 1-12

The above command will cut the byte range 1-12 from the given string and print out “cutting text” on the standard output. Providing byte ranges that are outside of the string’s occupation will result in a message showing “cut: invalid byte or character range”.

4. Cut Only a Single Byte from the Text file

$ cut -b 1 test.txt

This command will display only the first bytes of each of the five rows inside the file test.txt. It is equivalent to the command $ cat test.txt | cut -b 1

5. Cut Specific Bytes from the Text File

$ cut -b 1,3 test.txt

The above command cuts only the first and third bytes of each row. You can specify any byte numbers as long as they fall within the range of bytes available.

6. Cut Range of Bytes from the Text File

$ cut -b 1-12 test.txt

This command will output the first one to twelfth bytes of each row in the test.txt file. You should notice the similarity of functionality this command possess with the 3rd command.

7. Cut the First 7 Bytes in Alphabetical Order

$ cut -b 1-7 test.txt | sort

We can provide the output of the cut command as input to the sort command for displaying the first seven bytes of each row alphabetically. For alphabetical sorting, the sort command doesn’t require any options.

8. Cut the First 7 Bytes in Reverse Order

$ cut -b 1-7 test.txt | sort -r

This cut command will cut the first 7 bytes from each row and will output them in the reverse order. Look how the output of the cut command is being fed to the sort command using a pipe.

9. Cut from the Fifth Byte to the End of the Input Stream

$ echo "cutting text from input" | cut -b 5-

The above cut command will cut the text from the fifth byte to the end of the string. This command will come in handy when you need to cut from a specified byte position till the end of the input stream. Simply change the value of the b flag while keeping the trailing – on.

10. Cut from the Fifth Byte to the End of the File

$ cut -b 5- test.txt

This command will start cutting every one of the five rows of test.txt from the fifth-byte position and finish only after each row ends. The trailing hyphen(-) is mandatory for this operation.

11. Cut a Specified Amount of Bytes Starting from the First One

$ echo "cutting text from input" | cut -b -5

This command will cut the first five bytes of the input string. You can cut from the starting byte to any other byte position by just replacing the value of the b flag. Remember to add the preceding hyphen(-) else the output will not be as expected.

12. Cut from the First Byte to a Specified Position from The File

$ cut -b -5 test.txt

The above command will cut just the first five bytes of each line from our text file. Notice how the hyphen(-) is being used for the commands 9-12 in this list.

Cut Text Based on Characters


The cut command in Unix allows users to cut a section of text based on characters. When handling large file processing tasks, you’ll need to do this quite often. Try them out and notice the similarities between character-based cutting and byte-based cutting.

Linux cut command in Unix for cutting characters

13. Cut Only a Single Character from the Input Stream

$ echo "cutting text from input" | cut -c 1

The above command cuts the first character from the standard input and displays it in the terminal. In this case, it is “c“. Change your string to something different to understand this clearly.

14. Cut Specific Characters from the Input Stream

$ echo "cutting text from input" | cut -c 1,3

This command will cut only the first and third characters of the input string and show them. You can try cutting other characters but remember not to exceed the character limit of your string.

15. Cut Range of Characters from the Input Stream

$ echo "cutting text from input" | cut -c 1-12

In case of this command, “cut” will cut characters ranging from the first position to the twelfth position. The result will be “cutting text“. Note the similarities between this Linux cut command and the third command.

16. Cut Only a Single Character from the Text file

$ cut -c 1 test.txt

This command will display only the first characters of each of the five rows of our file test.txt. It is equivalent to the command $ cat test.txt | cut -c 1 and provides the same result as we would get when using the byte flag.

17. Cut Specific Characters from the Text File

$ cut -c 7,10 test.txt

The above command cuts only the seventh and tenth characters of each five rows. You can specify any character positions as long as they fall within the range of available characters.

18. Cut Range of Characters the Text File

$ cut -c 1-12 test.txt

This command will output the first one to twelfth characters of each line in the test.txt file. The cut command in Unix behaves the same when cutting a range of characters and range of bytes.

19. Cut the First 5 Characters in Alphabetical Order

$ cut -c 1-5 test.txt | sort

You can supply the output of the cut command as an input to the sort command for cutting the first five bytes of each row alphabetically. The sort command doesn’t require any options when sorting alphabetically.

20. Cut the First 5 Characters in Reverse Order

$ cut -c 1-5 test.txt | sort -r

This cut command will cut the first five characters from each row and will show them after sorting in reverse. Look how the output of the cut command is being fed to the sort command using a pipe.

21. Cut from the Fifth Character to the End of the Input Stream

$ echo "cutting text from input" | cut -c 5-

The above cut command will cut the text starting from the fifth byte to the end of the string. It can be beneficial when you need to cut from a specified character position until the end of the input stream. Simply change the value after b while keeping the trailing – on.

22. Cut from the Fifth Character to the End of the File

$ cut -c 5- test.txt

This command will start cutting each of the five rows of the test.txt file from their fifth character position and will finish after reaching the end of every line. The trailing hyphen(-) is mandatory for this kind of operation.

23. Cut a Specified Amount of Characters Starting from the First Position

$ echo "cutting text from input" | cut -c -5

This command will only cut the first five character positions of our input. You can cut from the starting character to any other character position by just substituting the value -c. Remember to add the preceding hyphen(-) else the output will not be the same as you expect.

24. Cut from the First Character to a Specified Position from The File

$ cut -c -5 test.txt

This cut command in Unix will cut the first five characters of each line from the file test.txt. Notice how the hyphen(-) is being used for the commands 21-24 in this list.

Cut Text From Columns using Fields and Delimiters


The cut command allows users to cut sections of a text very easily. For this, we need to use both the d and the f flag of cut. The d flag stands for delimiters and f for fields. Delimiters are special characters that separate section of a text from others. Common examples include ‘-‘, ‘:’, and ” ” (space). The reference file we’re using has ‘:’ as the separator.

Cutting Fields-and-Delimiters

25. Cut the First Section of Input Stream

$ echo "Let's cut this input stream section by section" | cut -d ' ' -f 1

The above cut command will cut the first section of text(“Let’s” in this case) from the input stream. Note that the value to the delimiter flag -d is a single space. Try it with text delimited by a colon and see what happens for yourself.

26. Cut the First Section of a File

$ cut -d ':' -f 1 test.txt

This command will return the first columns of each row inside our reference file and print the name of all the five countries. The value provided to the delimiter flag was a colon because that’s how our file separates the columns.

27. Cut Specific Sections of the Input Stream

$ echo "Let's cut this input stream section by section" | cut -d ' ' -f 1,2,3

Here we instructed cut to show only the first three field of the given input string. It is done using a comma-separated array of field positions. The output of this command will be ‘Let’s cut this‘.

28. Cut Specific Sections of a File

$ cut -d ':' -f 1,2,3 test.txt

This command will also provide the same sort of output as the previous command. Here, cut is just working on a file instead of the standard input, that’s all. It should show the name, capital, and currency of each country on the list. However, do notice the difference between their delimiters(space vs. colon).

29. Cut Range of Fields from the Input Stream

$ echo "Let's cut this input stream section by section" | cut -d ' ' -f 1-5

The above command will cut the first five fields of the string and display it in the terminal. The apostrophes are required when space is used as the delimiter between multiple fields.

30. Cut Range of Fields from a File

$ cut -d ':' -f 1-3 test.txt

This cut command will cut each of the first three columns of our text file and show it as the output. It should display the same result as provided by the command preceding the previous one. The apostrophes aren’t mandatory for characters like – or :.

31. Cut Each Entry from a Specific Field and List them Alphabetically

$ cut -d ':' -f 1 test.txt | awk '{print $1}' | sort

Suppose you need to find out the names of the five countries in our list in alphabetical order, you can utilize the above command for doing this. It will list the countries alphabetically sorted. A substitution in the value of the f flag will let you do this on other fields alike.

32. Cut Each Entry from a Field and List them in Alphabetically Reverse Order

$ cut -d ':' -f 1 test.txt | awk '{print $1}' | sort -r

This command does the same operation as the above one, just sorts the entries in a reverse manner, that’s all. The output is changed here due to the -r flag passed to sort.

33. Cut from a Fixed Field to the End of the Input Stream

$ echo "Let's cut this input stream section by section" | cut -d ' ' -f 2-

This cut command will cut starting from the second field to the end of the string. It can be beneficial when you need to cut from a specified position until the end of the input. You can change the value of -f while keeping the trailing – on for cutting from different fields.

34. Cut from a Fixed Field to the End of a File

$ cut -d ':' -f 2- test.txt

When used like this, the cut command will start cutting from the specified field and go till the end of each line. In this case, it will print out the capital, currency, and population of each of the five countries on the list.

35. Cut a Specified Number of Columns Starting from the First One

$ echo "Let's cut this input stream section by section" | cut -d ' ' -f -5

This command will only cut the first five fields of the given input. You can cut from the starting column to any other column position by just substituting the value -f. However, you need to add the preceding hyphen(-) else the output will not match your expectation.

36. Cut Some Specified Columns of a File Starting from the First One

$ cut -d ':' -f -2 test.txt

This Linux cut command will start cutting our file test.txt from the first column and terminate after it has finished cutting the second command. So, the output of this command will simply display the name of each country and their respective capitals.

37. Cut Multiple Fields of CSV Files

$ cut -d ',' -f 1,2 file.csv

The cut command will often prove to be a viable tool when you’re working with massive CSV documents. The above command, for example, will cut the first two columns of a comma-separated CSV file called file.csv.

38. Cut Specific Fields of CSV Files and Sort them in Reverse Order

$ cut -d ',' -f 1,3,5 file.csv | sort -r

The above command will cut the first, third, and fifth columns of a comma-separated CSV file named file.csv and display the output in the reverse order.

Some Miscellaneous Linux Cut Command for Experts


The cut command can be used for advanced file processing by utilizing it with appropriate filters, and other robust Linux commands. Below, we’ll go through some such commands that can benefit you in the long run.

Misc Linux Cut command in Unix

39. Inspect The passwd File Using Cut Command

$ cut -d ':' -f1 /etc/passwd

The passwd file stored inside /etc in most systems contain very sensitive information about the system and its users. You can inspect this file quickly using the cut command. Delimiter ‘:’ is used as the columns of this file are separated using it. Change the value of -f to monitor different fields.

40. Cut Specific Fields and Show Only the Unique Entries

$ cut -d ':' -f 3 test.txt | uniq -u

This cut command in Linux will cut the third column of the file test.txt and only show the unique entries. So for this file, the output will contain only three currencies – namely Euro, Lira, and Yen.

41. Cut All Bytes of Input Stream Except the Specified Ones

$ echo "Let's cut this input stream section by section" | cut -b 1,3,5,7 --complement

This cut command will cut all the characters of the given input string except the ones supplied to -b. So, byte positions first, third, fifth, and seventh will be omitted from the output.

42. Cut All Bytes of a File Except the Specified Ones

$ cut -b 2,4,6 test.txt --complement

When used in this manner, the cut command will cut all the bytes of the file test.txt except the one mentioned in the command. Thus, the output will not contain the second, fourth, and sixth bytes of each line.

43. Cut All Characters of Input Stream Except the Specified Ones

$ echo "Let's cut this input stream section by section" | cut -c 1,3,5,7 --complement

This command refrains from cutting the first, third, fifth, and seventh characters of the input string and instead cuts all other characters except these four.

44. Cut All Characters of a File Except the Specified Ones

$ cut -c 2,4,6 test.txt --complement

In case of this command, the output will contain all characters of the test.txt files except the ones mentioned. So, characters second, fourth, and sixth will not be displayed.

45. Cut all Input Sections Except the Ones Specified

$ echo "Let's cut this input stream section by section" | cut -d ' ' -f 1,3,5 --complement

The above command will output the string “cut input section by section“. So, it will display all the input sections without the ones mentioned after the field flag.

46. Cut All Columns of a File Except the Specified Ones

$ cut -d ':' -f 2,3 test.txt --complement

This command will cut only the first and last columns of the file test.txt. So, you can easily deselect some fields when processing large tabular documents using the complement flag.

47. Cut a Section of Input and Reverse them Characterwise

$ echo "Let's cut this input stream section by section" | rev | cut -d ' ' -f 1,3

The above Linux command will cut the first and third section of the input and reverse them characterwise. Notice, how the output of one command is being fed as the input to other commands.

48. Cut Specific Columns in a File and Reverse them Characterwise

$ cut -d ':' -f 1,3 test.txt | rev

This command will only cut the specified fields of the file test.txt and display the result in a characterwise reverse manner.

49. Modify the Output Delimiter of the Cut Command

$ echo "A,comma,separated,list,for,demonstration,purposes" | cut -d ',' -f 1- --output-delimiter=' '

Cut allows us to modify the output delimiter when displaying the result. The above command cuts all sections of the comma-separated list but replaces the commas with spaces when showing the result.

50. Example of Cut+Sed Command with Tab Delimiter

$ sed 's/:/\t/g' test.txt | cut -f 1-4

The last cut command of our list utilizes the mighty powerful sed utility to replace the colons in our file with tabs. You can replace \t with some other characters like – or ; for changing to an output delimiter of your choice.

Ending Thoughts


The cut command in Unix is a versatile tool which can aid numerous benefits for users who need to process large files frequently. We have outlined the 50 best Linux cut command for helping you get familiar with this fantastic utility. You should try them out individually and make modifications to the various available options. That will help you understand the different variations of the cut command in depth. Hopefully, we were successful in our quest to help you as much as possible. Stay with us for more upcoming guides on useful Linux commands.

Mehedi Hasan
Mehedi Hasan
Mehedi Hasan is a passionate enthusiast for technology. He admires all things tech and loves to help others understand the fundamentals of Linux, servers, networking, and computer security in an understandable way without overwhelming beginners. His articles are carefully crafted with this goal in mind - making complex topics more accessible.

You May Like It!

Trending Now