Skip to main content

Understanding the `cut` Command in Linux

The cut command in Linux is a straightforward utility that is used to extract sections from each line of files. It can be used to cut parts of a line by byte position, character and field. In other words, cut command is useful for slicing out columns and fields from a file or a stream of text data.

Syntax of cut Command

The basic syntax of the cut command is as follows:

cut OPTION... [FILE]...
  • OPTION... - One or more options that control the operation of the command.
  • [FILE]... - One or more files to process with the command. If no file is specified, or if the file is -, cut reads from the standard input.

Options for cut Command

The following table lists the common options available for the cut command:

--bytes=LIST-b LISTSelect only these bytes
--characters=LIST-c LISTSelect only these characters
--fields=LIST-f LISTSelect only these fields; also print any line that contains no delimiter character, unless the --only-delimited option is specified
--delimiter=DELIM-d DELIMUse DELIM instead of TAB for field delimiter
--only-delimited-sDo not print lines not containing delimiters
--output-delimiter=STRING--output-delimiter=STRINGUse STRING as the output delimiter the default is to use the input delimiter
--complement--complementComplement the set of selected bytes, characters or fields
--helpDisplay help and exit
--versionOutput version information and exit

Creating a Sample Text File Using vim

Before we proceed with examples, let's create a sample text file named data.txt using vim:

vim data.txt

In vim, press i to go into insert mode and type or paste the following content:

Alice:21:New York
Bob:30:San Francisco
Charlie:25:Los Angeles

To save and exit vim, press Esc, then type :wq and press Enter.

Examples of Using cut

Example 1: Cutting by Field

To cut out the first field of each line, using : as a delimiter:

cut -d ':' -f 1 data.txt

This command will output:


Example 2: Cutting by Character Range

To cut characters from 1 to 5 on each line:

cut -c 1-5 data.txt



Example 3: Cutting by Byte Position

To cut by bytes, you would use -b followed by the byte positions:

cut -b 1-5 data.txt

Example 4: Cutting Multiple Fields

To cut the first and third fields out of each line:

cut -d ':' -f 1,3 data.txt


Alice:New York
Bob:San Francisco
Charlie:Los Angeles

Example 5: Complementing the Selection

To cut everything except the first field:

cut -d ':' --complement -f 1 data.txt


21:New York
30:San Francisco
25:Los Angeles

Example 6: Cutting with a Different Output Delimiter

To cut the first field from each line and change the output delimiter to a comma:

cut -d ':' --output-delimiter=',' -f 1 data.txt



Example 7: Cutting Based on Character Range

To cut the second to fourth character on each line:

cut -c 2-4 data.txt



Example 8: Selecting Non-Delimited Lines

By default, cut will print lines even if they don't contain the delimiter. If you want to only show lines that contain the delimiter:

cut -d ':' -s -f 2 data.txt



The cut command can

be very powerful when used in conjunction with other text processing tools such as grep, sort, uniq, and awk. By mastering cut, you can effectively manipulate textual data in Linux, extracting and rearranging data as required.

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle