Cut


cut (remove sections from each line of files)

The cut command in RHEL 8 is a powerful utility for extracting specific sections of text from each line of a file. This functionality is essential for processing structured data, such as log files or CSV files, by allowing you to select columns or fields based on byte position, character, or a delimiter.


Example 1: cut -b

# This command cuts the first 4 bytes from each line of the 'employees.txt' file.
cut -b 1-4 employees.txt

Explanation

The -b option specifies that we are cutting by byte position. The range 1-4 instructs cut to extract the bytes from the beginning of the line up to the fourth byte for each line in the specified file.


Example 2: cut -c

# This command extracts the 3rd through the 7th character from each line of 'employees.txt'.
cut -c 3-7 employees.txt

Explanation

Using the -c option, we can select characters based on their position. This command will display a segment of each line from the third character to the seventh character, inclusive.


Example 3: cut -f with -d

# This command extracts the second field, using a comma as a delimiter.
cut -d ',' -f 2 employees.csv

Explanation

The -d option sets the delimiter, which is a comma in this case. Following that, -f 2 specifies that the second field should be extracted from each line of the employees.csv file.


Example 4: Extracting Multiple Fields

# This command selects the first and third fields based on a colon delimiter.
cut -d ':' -f 1,3 /etc/passwd

Explanation

You can extract multiple fields by separating the field numbers with a comma. This command is useful for pulling specific data, like usernames and user IDs, from the system's password file.


Example 5: Extracting a Range of Fields

# This command extracts a range of fields from the first to the third, using a space as the delimiter.
cut -d ' ' -f 1-3 access.log

Explanation

To extract a consecutive range of fields, you can use a hyphen. This example processes access.log, treating a space as the field separator and outputting the first three fields.


Example 6: Using --complement

# This command displays all fields except for the first and second fields, using a tab delimiter.
cut -f 1,2 --complement data.tsv

Explanation

The --complement option inverts the selection, printing everything but the specified fields. This is useful when you want to exclude a few specific columns from a file with many columns. By default, cut uses the tab character as a delimiter if the -d option is not specified.


Example 7: Combining with Other Commands

# This command lists the contents of the current directory and then extracts the permissions field.
ls -l | cut -c 1-10

Explanation

The cut command is frequently used with pipes (|) to process the output of other commands. In this instance, the detailed listing from ls -l is piped to cut, which then extracts the first ten characters representing the file permissions.


Example 8: Handling Lines Without Delimiters

# This command attempts to extract the second field using a semicolon delimiter, and the -s option suppresses lines that do not contain the delimiter.
cut -d ';' -f 2 -s mixed_data.txt

Explanation

The -s option is valuable for cleaning up output by preventing lines that lack the specified delimiter from being printed. This ensures that only lines with the intended field structure are processed.


While the cut command is a standard utility across most UNIX and Linux systems, including RHEL 8, its core functionality remains largely consistent. For instance, in both Solaris and macOS, the command syntax for the examples provided above would be identical. The fundamental options like -b, -c, -d, and -f are part of the POSIX standard, ensuring a high degree of compatibility for basic operations.