Linux cut Command Explained with 6 Examples

Introduction

The cut command is a command-line utility that allows you to cut out sections of a specified file or piped data and print the result to standard output. The command cuts parts of a line by field, delimiter, byte position, and character.

In this tutorial, you will learn what the cut command is and how to use it.

Linux cut command tutorial.

Prerequisites

  • A system running Linux.
  • Access to a terminal.

cut Command Syntax

The cut command takes the following syntax:

cut [option] [file]

Options

Specifying an [option] is necessary. Otherwise, the command outputs an error. Available options are described in the following section.

File

For [file], enter the name of the file you want to process. Not specifying a filename instructs the cut command to read from the standard input, in which case cut filters the pipeline. If you specify multiple filenames, the cut command concatenates the requested content.

Note: Another Linux command that produces formatted outputs is the awk command.

cut Command Options

The cut command options provide instructions on using a delimiter, cutting by byte position, field, or character. Use a single option for each cut command you run.

The available options are:

Option Description
-f (--fields=LIST) Select using a specified field, a field set, or a field range.
-b (--bytes=LIST) Select using a specified byte, a byte set, or a byte range.
-c (--characters=LIST) Select using a specified character, a character set, or a character range.
-d (--delimiter) Used to specify a delimiter to use instead of the default TAB delimiter.
--complement When specified, this option instructs cut to display all the bytes, characters, or fields, except the selected.
-s (--only-delimited) The default setting is to print the lines that don’t contain delimiter characters. Specifying the -s option instructs cut not to print the lines that don’t contain delimiters.
--output-delimiter By default, cut uses the input delimiter as the output delimiter. Specifying the --output-delimiter option allows you to specify a different output delimiter.

The -f, b, and -c options take the LIST argument, which is one of the following:

  • An integer N representing a byte, field or character, starting from 1.
  • Multiple integers, comma-separated.
  • A range of integers.
  • Multiple integer ranges, comma-separated.

Each range can be one of the following:

  • N- – Starts from the integer N (field, byte or character) up to the end of the line.
  • N-M – From the integer N up to integer M, inclusive.
  • -M – From the first field, byte, or character, up to the specified M field, byte, or character.

Linux cut Examples

Below are the most common cut command use cases.

Important: Pay attention to the locale of the file/command output you are processing. Cutting characters or bytes in a language other than English may produce an incorrect output if the character in question is longer than one byte.

Cut by Bytes

The -b option allows you to extract data using bytes. The syntax is:

cut -b [LIST] [file]

The [LIST] argument are the bytes to extract from each line of [file].

Depending on what you want to extract, you can cut a single byte, multiple bytes, or a byte range. To cut from a specific file, specify the filename at the end of the command. For example, we will use employees.txt for testing:

Using the cat command to show a file's contents.

Note: Use the cat command to show a file’s contents in the terminal.

To extract the first byte from each input line, run:

cut -b 1 employees.txt
Extracting a specific byte from a file.

The command prints only the first byte from each file input line.

The following example shows how to pipe into the who command and extract the first 5 bytes from the output:

who | cut -b -5
Extracting bytes from the who command output.

Cut by Characters

To cut by characters, specify the -c option. Cutting by characters is similar to cutting by bytes, except you need to specify the character position rather than the byte position. The syntax is:

cut -c [LIST] [file]

The [LIST] argument specifies the characters to be extracted from each line of [file].

For example:

cut -c 10- employees.txt
Extracting specific character from a file using cut.

The command extracts everything from character 10 until the end of the line from each line of employees.txt. The results are printed in standard output.

The following example shows the result when a file isn’t specified and cut reads its input from standard input. Take a look at the who command output:

An example of the who command output.

The output indicates that one user is currently logged in. Use the cut command to extract the logged-in user’s username from the who command’s output:

who | cut -c 1-8
Extracting usernames from who command output using cut.

In the example above, we instruct cut to extract characters 1 through 8 from each line of input. In case of multiple results, sort the results by appending the command with | sort.

Note: To know the character position, run the who command first and count out the appropriate character positions. The who command’s output has a fixed format, so the character positions don’t change. However, data isn’t always organized in a fixed manner in every command output, so make sure to check the output before piping it.

Additionally, use cut to extract multiple different characters from a line. For example, display the username and login time of all logged-in users:

who | cut -c 1-8,18-
Extracting multiple characters from a line using cut.

Cut Based on a Delimiter

If the fields aren’t separated by the default tab character, use the -d option to specify a different delimiter. That means that the character specified after the -d option is considered the separator in the lines. The syntax is:

cut -d[delimiter] [file]

In place of the [delimiter] argument, specify the delimiter you want. You can use any character as a delimiter. Using the cut command to extract fields from a file without specifying the -d option means that the default delimiter is the tab character.

In the following example, we use whitespace as a delimiter and print the second field:

echo "phoenixNAP is a global IT services provider" | cut -d ' ' -f 2
Extracting fields from a file using cut and a delimiter.

Note: The echo command prints out a text string you provide as an output message.

Cut by Fields

When piping into commands whose output doesn’t have a fixed format (e.g., the who command), the -c option isn’t helpful. Using the -f option to separate by fields is a better choice in that case.

For example:

cut -f 2 employees.txt
Extracting text from a file using fields.

In the example above, we used the -f option to extract the second field from the employees.txt file.

To cut specific fields from a file, specify a different delimiter. For example, the /etc/passwd file output contains all the users on your system, id numbers, home directory, etc.

Output of the /etc/passwd file.

The data in the /etc/passwd file isn’t aligned in the same way as the data in the who command output. Thus, you cannot extract all the users on the system by relying on the character number.

However, the fields in the /etc/passwd file are delimited by a colon. Hence, count the number of colons to extract the same fields. For example:

cut -d: -f1,6 /etc/passwd
Filtering the /etc/passwd file output using the cut command.

The output returns each user in the system and their home directory, corresponding to fields 1 and 6, respectively.

Complement a Selection

The --complement option prints everything except for the character/byte/field at the specified position. For example, the following command prints all fields except the first and third:

cut employees.txt -f 1 --complement
An example of the cut --complement option.

Specify an Output Delimiter

When specifying multiple characters/bytes/fields, the cut command concatenates the output without a delimiter. Specify a delimiter in the output using the --output-delimiter option.

For example, to set the output delimiter to _ (underscore), use:

cut employees.txt -f 1,3 --output-delimiter='_'
Specifying an output delimiter in the cut command.

Note: If you are just starting with Linux commands, take a look at the comprehensive overview in our article Linux Commands Cheat Sheet.

Conclusion

You now know what the Linux cut command is and how to use it to process a file or command output. Feel free to test out the different options to get used to them and maximize your use of the command line for manipulating data and command outputs.

原创文章,作者:bd101bd101,如若转载,请注明出处:https://blog.ytso.com/224680.html

(0)
上一篇 2022年1月7日
下一篇 2022年1月7日

相关推荐

发表回复

登录后才能评论