The Cut Command in Linux [6 Practical Examples]

Try this guide with our instant dedicated server for as low as 40 Euros

Cut Command in Linux

The cut command in Linux is a powerful utility for extracting specific text sections from files or command output. It’s particularly handy for working with structured data, like the contents of a passwd file. 

Cut excels at quickly extracting and displaying the necessary information from the standard input or the specified file. It’s a go-to choice for parsing and manipulating text concisely and efficiently.

This utility allows precise text manipulation using a delimiter character, often a tab character, to identify text blocks. Cut can isolate specific character positions or fields within a line. You have very fine control over the process and can extract single bytes, define a starting position, or select entries through a comma-separated list. 

The cut utility is an important part of the sysadmins’ toolkit because of its versatility in handling and modifying textual data in Linux environments. 

In this article, we’ll discuss the cut utility and then go into the details of six use cases where the cut utility helps extract specific sections from text or files.

Table Of Contents

  1. The Cut Command Basic Syntax
    1. Cut Command Options
  2. Examples of Linux Cut Command
    1. Prerequisites
    2. Example #1: Cutting Text by Specific Bytes
    3. Example #2: Cutting Text by Particular Characters
    4. Example #3: Cutting Text Based on a Delimiter
    5. Example #4: Cutting Text by Fields
    6. Example #5: Complementing a Selection
    7. Example #6: Specifying an Output Delimiter
  3. Conclusion
  4. FAQs

The Cut Command Basic Syntax

The basic syntax of the cut command is:

# cut OPTION... [FILE]...

Cut Command Options

The cut command supports several options. The following are the most common options:

cut command options

Examples of Linux Cut Command

Before delving into the practical examples, let’s see what you need to start working with the cut command in Linux.

Prerequisites

Before diving into the cut command, ensure you have the following: 

  • Basic understanding of the Linux command line interface.
  • Access to a Linux system to practice the examples.

Example #1: Cutting Text by Specific Bytes

The byte-based extraction feature of the cut command allows for the selection of specific bytes from each line in a file. 

This method lets you easily pinpoint and retrieve specific byte positions within a file’s content. This approach is particularly useful when dealing with files where data is arranged in a consistent byte format.

We’ll start by using the cat command to display the content of the redswitchesemployess.txt file:

# cat Marketing_Department_emp.txt

cat marketing department

Here’s the cut command we’ll use for extracting the desired information:

# cut -b [LIST] [FILE]

In this syntax, the -b option specifies byte positions for extraction. So, this command indicates that you want to extract specific bytes from the specified file based on the byte positions provided in the list. 

For instance, the following command extracts the first byte (the first letter in this case):

# cut -b 1 Marketing_Department_emp.txt

cut b1 marketing department

In the next example, we’ll use the cut command with the who command and get the first five bytes of the output:

# who | cut -b -5

cut b-5

Example #2: Cutting Text by Particular Characters

You can specify characters for extracting the relevant information with the cut command. 

This feature is often used to extract specific characters from each line in a text file. It’s perfect when you want to pick out characters that follow a consistent pattern across different lines. In this context, the cut command enables precise selection and extraction of character sequences.

The syntax of the command is:

# cut -c [LIST] [FILE]

The [LIST] parameter determines which characters should be selected from each line in the [file].

For instance, the following command selects all characters starting from the 10th character to the end of each line in the employees.txt file. The output is displayed on the standard output.

# cut -c 3- Marketing_Department_emp.txt

cut c3 marketing department

In the next example, we’ll demonstrate how to apply the cut command on the standard input. Let’s start with the standard output of the who command: 

# who

who command output

The result shows that a single user is logged in at the moment. To isolate the username of this logged-in user from the output of the command, we’ll apply the cut command as follows:

# who | cut -c 1-8

who cut c 1 8

In this example, the cut command is configured to select characters 1 to 8 from every input line. If there are multiple entries, you can organize them by adding | sort to the command.

Furthermore, the cut command can select various distinct characters from a line. For instance, to show the username and login time of all users currently logged in, you can use:

# who | cut -c 1-4,10-

who cut c 1 4,10

Example #3: Cutting Text Based on a Delimiter

The delimiter-based text extraction feature is particularly useful for files with a structured format, such as CSV files. 

You can use the -d option to define the delimiter and the -f option to select the field number you wish to extract. This functionality simplifies extracting specific data from columns in delimited files.

The syntax of the cut command would be:

# cut -d [DELIMITER] -f [FIELD_LIST] [FILE]

Replace the [DELIMITER] argument with your chosen delimiter, which can be any valid character. Note that the default delimiter is set to the tab character. If you wish to use this default option, you can omit the -d option.

In the following examples, we’ve used whitespace as a delimiter to print the third field of the input:

# echo "RedSwitches Bare Metal Server Provider" | cut -d ' ' -f 3

Similarly, the following command prints the fourth field:

# echo "RedSwitches Bare Metal Server Provider" | cut -d ' ' -f 4

echo redswitches bare metal server provider

Example #4: Cutting Text by Fields

You can effortlessly extract complete fields from files organized in a columnar format with the -f option. This is especially useful for handling data in table-like structures. The syntax of the command is:

# cut -f [FIELD_LIST] [FILE]

For instance, consider the following command:

# cut -f 3 Marketing_Department_emp.txt

cut f 3 marketing department

The -f option selects the second field from the employees.txt file. 

You can specify an alternate delimiter to extract particular fields from a file. For instance, the output from the /etc/passwd file includes comprehensive information (such as ID numbers and home directories) about users on your system. Here’s the command to view the contents of this file:

# cat /etc/passwd

cut etc pwd

You’ll notice that the data in the /etc/passwd file is arranged differently from the output of the who command. So, it’s difficult to identify all system users simply by character count.

The fields in the /etc/passwd file are separated by a colon (:). So, you can accurately extract specific fields by counting the colon in the file. 

For instance, consider the following command:

# cut -d: -f1,9 /etc/passwd

cut f1 etc pwd

The output displays every user on the system and their home directory, corresponding to fields 1 and 6, respectively.

Example #5: Complementing a Selection

The –complement feature in the cut command is used to select everything other than the specified bytes, characters, or fields.

The syntax of the command is as follows:

# cut --complement OPTION [FILE]

For instance, the commands below output all fields except for the first and second:

# cut Marketing_Department_emp.txt -f 1 --complement

# cut Marketing_Department_emp.txt -f 2 --complement

cut marketing department complement

Example #6: Specifying an Output Delimiter

You can change the delimiter used in the output with the –output-delimiter option. This provides flexibility in formatting the extracted data, mainly when working with various input and output formats.

The syntax of the command is:

# cut -d [INPUT_DELIMITER] --output-delimiter=[OUTPUT_DELIMITER] -f [FIELDS] [FILE]

As an example, if you want to change the output delimiter to an underscore (_), you would use:

# cut Marketing_Department_emp.txt  -f 1,2 --output-delimiter='_'

# cut Marketing_Department_emp.txt  -f 1,3 --output-delimiter='_'

cut employees

Conclusion

The cut command in Linux is super handy with its default space delimiter. It adeptly pulls out specific parts of files, like the first field or any range you want. You can define this range using characters, bytes, or a list of numbers, all separated by commas. This makes it versatile for handling various file formats and content extraction tasks.

This command, often paired with the echo command for testing, prints a specified range of characters or list of bytes from file content, adeptly handling regular expressions and complement patterns. 

The flexibility to specify a range of integers or a comma-separated list enhances its functionality, making it a go-to tool for the root user or any other user needing to parse and extract specific portions from a sample file or any textual data.

RedSwitches helps customers by offering customizable bare metal servers. If you’re searching for a reliable server infrastructure for your projects, we provide competitive, dedicated server pricing and ensure swift delivery, typically on the same day of order approval. Whether you require a dedicated servers, a traffic-friendly 10Gbps dedicated server, or a high-performance instant dedicated server, we are your trusted hosting partner!

FAQs

Q. What is the Linux cut command used for?

The cut command in Linux extracts text sections from each line in files or data streams based on specified bytes, characters, or fields.

Q. Can cut work with different types of delimiters?

Yes, the cut command can work with different delimiters using the -d option.

Q. Is it possible to invert the selection with the cut command?

Yes, you can invert the selection using the –complement option, which selects everything except the specified bytes, characters, or fields.

Q. Can the cut command modify the output delimiter?

Yes, the cut command can change the output delimiter using the –output-delimiter option.

Q. Is the cut command suitable for beginners?

Yes, the cut command is straightforward and suitable for beginners, especially with a basic understanding of the Linux command line.

Q. What is the cut command in Linux?

The cut command in Linux extracts sections from each line of input and writes the result to standard output.

Q. How do I use the cut command in Linux?

You can use the cut command in Linux by specifying the range of bytes, characters, or fields and the input source (file or piped data).

Q. Can you provide an example of the cut command in Linux?

Sure, for example, you can use the following command: `cut -d”,” -f1,3 filename.csv` to extract the first and third fields from a CSV file using a comma as the delimiter.

Q. What are some practical examples of using the cut command in Linux?

Some practical examples of using the cut command in Linux include extracting specific columns from a text file, parsing log files, and processing delimited data.

Q. How does the cut command in Linux work with the Unix pipe?

The cut command can be used with the Unix pipe to take the output from a preceding command and use it as the input for the cut command, allowing you to manipulate the data further.

Q. What is the purpose of the cut command in Unix with examples?

The purpose of the cut command in Unix is to extract specific portions of text data, such as columns in a table or delimited fields in a text file. Examples include extracting the login names and home directories from the /etc/passwd file.

Q. What does the cut utility allow you to do in Linux?

The cut utility in Linux allows you to cut parts of lines from a file and print the result to standard output based on specified delimiters and fields.

Q. How can I use the cut command in Unix to extract specific fields from a file?

You can use the cut command in Unix to extract specific fields from a file by specifying the delimiter and the field number, such as using `-d’,’ -f2` to extract the second field in a comma-delimited file.

Q. In what scenarios is the cut command in Linux commonly used?

The cut command in Linux is commonly used in data processing, text manipulation, working with structured data, and parsing output from other commands.

Q. How does the cut command in Linux differ from the grep command?

The cut command in Linux extracts specific columns or fields from a file, while the grep command searches for specific patterns or lines of text within a file.

Try this guide with our instant dedicated server for as low as 40 Euros