cut Command in Linux

cut Command in Linux

Learn the cut command in Linux with practical examples on text processing.

In this article, we explain the cut command in Linux with practical examples on text processing.

What is Linux?

Linux is a family of open-source operating systems based on the Linux kernel. The first Linux system kernel was released on September 17, 1991, by Linus Torvalds.

Read more …

Popular Linux distributions include Debian, Fedora, and Ubuntu, and the commercial distributions include Red Hat Enterprise Linux and SUSE Linux Enterprise Server.

There are also quite a number of customized Linux distributions, such as Kali Linux, REMnux etc. Kali Linux is a Debian-based distribution developed, funded and maintained by Offensive Security for ethical hackers for the purposes of Penetration Testing, Security Research & Assessment, and Computer Computer Forensics & Reverse Engineering. REMnux, on the other hand, is a Linux distro curated for reverse-engineering and malware analysis purposes.

Quote by Linus Torvalds
Quote by Linus Torvalds

What I find most interesting is how people really have taken Linux and used it in ways and attributes and motivations that I never felt.

Linus Torvalds

Read more educational and inspirational cyber quotes at our page 100+ Best Cyber Security & Hacker Quotes.

cut Command in Linux

In Linux, cut is a command-line utility to extract sections of lines from specified files or piped data and print the extracted sections to the standard output. The parts to be cut from the lines of text can be specified by field, character or byte.

Print selected parts of lines from each FILE to standart output (stdout). Basic usage: cut OPTION ... [FILE] ... Use -d to specify a delimeter instead of the default field delimiter of TAB, use -f to select the fields to be printed to standard output.

Using the cut Command

With cut command, sections of text can be extracted by field, character position or byte position. Note that the text extraction method is specified as a mandatory parameter when using the cut command. Frequently used cut command options are described below and explained with practical examples in the following sections.

  • -b: Select only these bytes.
  • -c: Select only these characters.
  • -f: Select only these fields.
  • -d: Use the specified delimiter instead of the default TAB delimiter.
  • –complement: Complement the set of selected bytes, characters or fields.
  • –output-delimiter: Use the specified STRING as the output delimiter instead of the the default input delimiter.

Cut by Fields

To extract one or more fields from a line of text and print the output to the standard output, specify the field number(s) after the -f parameter.

For instance, to extract the 4th and 6th fields separated by space characters from a piped input, use the following command.

$ echo "Learning Linux is easy and fun" | cut -d ' ' -f 4,6
Figure 1: Cut by Fields Delimited by Space Characters
Figure 1. Cut by Fields Delimited by Space Characters

To extract the user names, user home directories and user login shells of the first 5 users listed in the /etc/passwd file, use the following command:

$ cat /etc/passwd | head -n 5 | cut -d ':' -f 1,6,7
Figure 2. /etc/passwd File Format in Linux
Figure 3. Cut by Fields Delimited by Colon Characters

To replace the default output delimiter in the previous example with a right arrow symbol, use the --output-delimiter=STRING parameter, as shown below.

$ cat /etc/passwd | head -n 5 | cut -d ':' -f 1,6,7 --output-delimiter='->'
Figure 4. Specifying the Output Delimiter

Cut by Characters

To extract and print one or more characters from a line of text, specify a list of character position(s) after the -c parameter. Character positions (numbers) in the list can be separated by comma (,) to specify discrete positions, or can be separated by hyphen (-) to denote a range of character positions.

For instance, to extract the first 4 characters of the username kali defined in the /etc/passwd file, use the following command:

$ cat /etc/passwd | grep kali | cut -c -4
Figure 5. Extracting a Range of Characters by Specifying Only the Ending Index

To extract a range of characters, i.e., the home directory for the kali user, by specifying starting and ending indices from the output of the previous example, use the following command:

$ cat /etc/passwd | grep kali | cut -c 26-35
Figure 6. Extracting a Range of Characters by Specifying the Starting and Ending Indices

To extract a range of characters by only specifying the starting index, i.e., to extract the user login shell for the kali user from the /etc/passwd file, use the following command:

$ cat /etc/passwd | grep kali | cut -c 37-
Figure 7. Extracting a Range of Characters by Specifying Only the Starting Index

Cut by Bytes

To extract and print one or more bytes from a line of text, specify a list of byte position(s) after the -b parameter. Byte positions (numbers) in the list can be separated by comma (,) to specify discrete positions, or can be separated by hyphen (-) to denote a range of byte positions.

For instance, to extract the first 4 bytes of the username kali defined in the /etc/passwd file, use the following command:

$ cat /etc/passwd | grep kali | cut -b -4
Figure 8. Extracting a Range of Bytes by Specifying Only the Ending Index

To complement the set of selected bytes from the previous example, use the --complement parameter, as in the following example:

$ cat /etc/passwd | grep kali | cut -b -4 --complement
Figure 9. Complementing the Set of Selected Bytes

To explore more cut command options, please visit the man pages by typing $ man cut on the terminal.

To learn more on Linux, you could also visit our Linux Resources Page.