Table of Contents
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
-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
--bytes option allows you to select bytes and print them in the output.
$ cut -b N file
$ cut --bytes=N file
For example, if we select 1 byte, it prints the 1st byte only.
You can select multiple bytes using a comma.
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
To print a range of bytes from 4-11, you can use:
cut command also accepts multiple files to select bytes, characters, or fields.
3. Print only selected characters with cut command
You can use
--characters option to cut by characters and print those characters. TAB and Space are counted as 1 character.
$ cut -c N file
$ cut --characters=N file
Here, we are selecting a range of characters from 2-12.
4. cut command to print only selected fields
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
$ cut -f N file
There is no TAB in the lines, so all lines are printed.
When we include the TAB in the text file, the output is changed.
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.
$ cut -b N- file
$ cut -c N- file
$ cut -f N- file
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.
$ cut -b -M file
$ cut -c -M file
$ cut -f -M file
7. Use different delimiter with cut command
By default, cut uses a TAB for a field delimiter.
--delimiter option lets you use a different delimiter instead of TAB. The delimiter must be a single character.
$ cut -d DELIM -f N file
$ cut --delimiter=DELIM -f N file
-d option specifies a delimiter 'e'. A delimiter may be specified only when operating on fields.
8. cut command to print only delimited characters
-f option also prints the line that does not contain the delimiter character. If we use
--only-delimited option, it will not print lines that do not contain a delimiter.
$ cut -sf n file
$ cut --only-delimited -f n file
The first line does not contain a delimiter 'h'. So, it will not be printed when
-s option is used.
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
$ cut --complement -b N file
10. Display the version of cut with cut command
You can view the version information of cut using
$ cut --version
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
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.