10+ cut command examples in Linux [Cheat Sheet]


CheatSheet

Reviewer: Deepak Prasad

Introduction to cut command

cut is a command-line utility to remove sections from each line of files in the Linux system. It prints selected parts of lines from each file to standard output.

 

How cut command works

You must specify a list of bytes, characters, or fields with cut command. So, cut only works when -b, -c, or -f option is used.

You can use the following methods to select bytes, characters, or fields.

  • N : N'th byte, character, or field, counted from 1
  • N-M : from N'th to M'th byte, character, or field
  • N- : from N'th byte, character, or field, to end of line
  • -M : from 1st to M'th byte, character, or field

 

Different examples to use cut command

Now, let's see some practical examples of cut commands using different available options.

The general syntax of cut command is:

$ cut OPTION... [FILE]...

 

1. Print only selected bytes using cut command

-b or --bytes option allows you to select bytes and print them in the output.

$ cut -b N file

OR

$ cut --bytes=N file

Sample Output:

For example, if we select 1 byte, it prints the 1st byte only.

cut command to select bytes

You can select multiple bytes using a comma.

cut command to select multiple bytes

 

2. cut command to select a range of bytes

You can select a list of ranges using the hyphen(-). TAB and Space are counted as 1 byte.

$ cut -b N-M file

Sample Output:

To print a range of bytes from 4-11, you can use:

cut command to select a range of bytes

cut command also accepts multiple files to select bytes, characters, or fields.

cut command to select bytes from multiple files

 

3. Print only selected characters with cut command

You can use -c or --characters option to cut by characters and print those characters. TAB and Space are counted as 1 character.

$ cut -c N file

OR

$ cut --characters=N file

Sample Output:

Here, we are selecting a range of characters from 2-12.

cut command to cut by characters

 

4. cut command to print only selected fields

-f or fields option allows you to cut by fields. It also prints any line that contains no delimiter character. A delimiter is a character that marks the beginning and end of a line. TAB is the default field delimiter.

$ cut -f N file

OR

$ cut -f N file

Sample Output:

There is no TAB in the lines, so all lines are printed.

cut command to cut by fields

When we include the TAB in the text file, the output is changed.

cut command to cut by fields

 

5. cut command to select a range from N'th to the end

To select a range from N'th to the end byte, characters, or fields, you can use the following command.

For bytes:

$ cut -b N- file

For characters:

$ cut -c N- file

For fields:

$ cut -f N- file

Sample Output:

cut command to select a range of bytes, characters or fields

 

6. cut command to select a range from the first to M'th

Similarly, to select a range from the first to the M'th byte,s characters, or fields, you can use the following command.

For bytes:

$ cut -b -M file

For characters:

$ cut -c -M file

For fields.

$ cut -f -M file

Sample Output:

cut command to select a range of bytes, characters or fields

 

7. Use different delimiter with cut command

By default, cut uses a TAB for a field delimiter. -d or --delimiter option lets you use a different delimiter instead of TAB. The delimiter must be a single character.

$ cut -d DELIM -f N file

OR

$ cut --delimiter=DELIM -f N file

Sample Output:

Here, -d option specifies a delimiter 'e'. A delimiter may be specified only when operating on fields.

cut command to use different delimiter

 

8. cut command to print only delimited characters

-f option also prints the line that does not contain the delimiter character. If we use -s or --only-delimited option, it will not print lines that do not contain a delimiter.

$ cut -sf n file

OR

$ cut --only-delimited -f n file

Sample Output:

The first line does not contain a delimiter 'h'. So, it will not be printed when -s option is used.

cut command to print only lines containing delimiter

 

9. cut command to complement the set of selected bytes, characters, or fields

--complement option is used to complement the output. It prints every line except the selected bytes, characters, or fields. It can be used with option -b, -c, or -f.

$ cut --complement -b N file

Sample Output:

cut command to complement the set of selected bytes, characters or fields

 

10. Display the version of cut with cut command

You can view the version information of cut using --version option.

$ cut --version

Sample Output:

cut command to display version information

 

11. cut command to display help message

--help option displays the help message that contains the available options and their uses in cut command.

$ cut --help

Sample Output:

cut command to display help message

 

Conclusion

We hope you have learned from this tutorial to use cut command. cut command prints the selected bytes, characters, or fields from each line of a file. If you still have any confusion, ask us in the comment section.

 

What's Next

10+ cmp command examples in Linux [Cheat Sheet]

 

Futher Reading

man page for cut command

 

Rohan Timalsina

Rohan Timalsina

He is proficient in a wide range of skills, including Page Builder Plugins such as Elementor, Beaver Builder, Visual Composer, and Divi Builder. His expertise extends to Front End Development with HTML5/CSS3, JavaScript, Bootstrap, and React.js. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment