cut Command in Linux with Useful Examples

Last updated: August 11, 2022

The Linux operating system has plethora of tools for manipulating text within files as well as piped output from other commands. One of the tools we will look at in depth in this article is the cut command.

As a Linux power user or system administrator, you are constantly working with log files, so knowing these commands is useful when parsing text data from logs or commands.

In this tutorial, we learn about cut command in Linux with examples.

What is the cut command in bash?

The cut is a Linux/Unix command line utility that removes sections of lines from files as well as piped data and prints the selected parts of lines to standard output (stdout) that is your terminal screen.

The syntax of the cut command is very simple:

cut OPTION... [FILE]...

Cut command option description

In this section, we will break down the cut command syntax and explain some important options that you can use with the cut command.

The OPTION are command-line arguments that change how the cut command behaves. For instance, you can tell the cut command what character to use to delimit the fields from a given text or set of data.

The following are some of the most important cut command options:

  • -b (--bytes=LIST) - this indicates which byte, bytes, or range of bytes to select.
  • -c (--characters=LIST) - instead of select bytes you can use the option to only select the characters you want.
  • -d (--delimiter=DELIM) - instead of using the TAB as the default delimiter character you can specify a character (DILIM) to use as the delimiter.
  • -f (--fields=LIST) - together with the -d option, it is the most used option. This option allows you to select the fields you want.
  • --complement - this complement the set of bytes, characters or fields you have selected. For example, it allows the cut command to display the bytes, characters, or fields except for the ones you have selected.
  • -s (--only-delimited) - by default, the cut display lines with no delimiter character. This option prevents the cut command from printing lines that do not contain delimiters.

--output-delimiter=STRING - As the output delimiter, use STRING. The input delimiter is used by default.

The cut command can read data from one or more FILEs. If there is no FILE  specified, or if the FILE is -, then the cut command reads the data from standard input, which is data piped (|) from other commands or data provided by input redirection (<).

Keep in mind that you should only use one of the option -b, -c, or -f. The -b, -c, and -f options each receive a LIST that can contain a single integer range or several integer ranges separated by commas. Additionally, the selected input is written exactly once in the same sequence as it is read.

Each specified range is one of:

  • N - an integer counted from 1 which specify N'th byte, character or field to select.
  • N- - selects from the N'th byte, character or field, up to the end of line.
  • N-M - selects from the N'th to the M'th (included) byte, character or field.
  • -M - select from first to M'th (included) byte, character or field.

Cut Options with Examples

Now that you are aware of and understand all the options available when using the cut command, let's look at some examples of its application.

-b option

We can use the -b option of the command to extract bytes or a range of bytes from a given file or text piped from other command output.

Let's look at the following command, which  get a single letter from a given data that is present on a specific byte position, in this case position 17.

echo "abcdefghijklmnopqrstuvwxyz" | cut -b 17
extract the specific bytes

The above output clearly shows that the character 'q' is present on the 17th byte of a string. Remember that we can select multiple bytes by separating the values to select with commas. The following example demonstrates this clearly.

This will select the 1st byte, 2nd byte and 3rd byte.

echo "abcdefghijklmnopqrstuvwxyz" | cut -b 1,2,3
extract the selected bytes

We can also get the same results by making use of ranges, here is a quick example to show that:

echo "abcdefghijklmnopqrstuvwxyz" | cut -b 1-3
extract the range of bytes

It is also possible to select multiple integer ranges by separating them with commas.

echo "abcdefghijklmnopqrstuvwxyz" | cut -b 1-3,8-10
extract the multiple range of bytes

It is important to know that we can ignore the starting position or the ending position while specifying the range. Here is an example of selecting a single byte by just specifying the ending position:

echo "abcdefghijklmnopqrstuvwxyz" | cut -b -1
cut -d

We can cut up to the end of the line by simply omitting the ending position. Here is an example:

echo "abcdefghijklmnopqrstuvwxyz" | cut -b 1-

-c option

When the -c option is used, the cut command behaves in a similar way as when the -b option is used. So all the examples we discussed with the -b option works the same with this option.

Now let us move to move to our next option.

-d and -f options

The TAB character is used as a field delimiter by default, but the cut command allows us to use any other character as a delimiter. The delimiter characters can be any character of your choice.

The -d option of the command is used to specify a delimiter, and the -f option is used to specify the field number to be selected.

In the following example, we will only extract the time the system has been running along with the current time from the output of the command uptime. Because the fields are separated by commas, we'll use the comma as a delimiter.

uptime | cut -d "," -f 1

You can also select multiple fields by separating the field numbers with commas or by defining ranges to select with hyphens. I'm not going to talk about it because we've already discussed it in previous examples, so feel free to experiment on your own.

--complement option

Only the bytes, characters, or fields that are not selected will be printed with the --complement option.

Here's an example of printing all fields except the ones you've chosen. The first and second fields will not be printed in this case:

uptime | cut -d "," -f 1,2 --complement
cut complement

--output-delimiter option

If you choose more than one field, the input delimiter will be used to separate them. You can change this behavior by using the cut command output delimiter option.

Here is a quick example of changing our input delimiter from `,` to '~':

uptime | cut -d "," -f 1,2 --output-delimiter='~'
cut output-delimiter

More Examples

As you can see, the cut command is frequently used in conjunction with other commands via a command line feature known as piping.

Some useful cut command with delimiter examples:

Example 1: Get the last Field of a string

echo 'example.com' | rev | cut -d'.' -f 1 | rev
Output
com

Example 2: Get the first column

echo 'landscape:x:109:115::/var/lib/landscape:/usr/sbin/nologin' | cut -d: -f1
Output
landscape

Example 3: Remove multiple columns

Here Column 3 and column 5 are removed

echo '1column 2column 3column 4column 5column 6column' | cut -d" "  -f1-2,4,6-
Output
1column 2column 4column 6colum

Example 3: Get login shells of all users

To get the login shell of all the users on the system use the following command:

getent passwd | rev | cut -d ":" -f 1 | rev | grep -v "nologin"

By combining the cut command with other useful commands, we were able to obtain all of the login shells of all users while ignoring those without a login shell.

Example 4: Getting all system users

We already got the login shells, so let's look at an example of getting the usernames on the system.

In this example instead of using the getent passwd command, we will try another way of viewing the /etc/passwd file using any file viewer command.

cat /etc/passwd | cut -d ":" -f 1

In the above examples we used the single character ":" as our delimiter since the entries in the /etc/passwd file are separated it. Also notice, we have piped the cat command output to the cut command.

Conclusion

That concludes this tutorial; thank you for taking the time to read. We learned how to use the cut command in Linux in this tutorial. We've gone over its options with some real-world examples.

For more information on the cut command, simply type man cut into your terminal.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin started his career in IT in the year 2000 as a computer science instructor and worked as a Linux system engineer and senior analyst roles. Currently working in DevOps environments to increase efficiency and improve delivery time in AWS Cloud infrastructure. He is certified in RHEL, CCNA, and MCP and holds a Masters's in computer science. When in his free time love playing cricket, blogging, and listening to music.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment