2024-10-05
cut
CommandThe cut
command operates by selecting portions of lines based on specified delimiters or byte/character positions. Its basic syntax is:
cut [OPTIONS] [FILE]...
The key options are:
-b
(bytes): Selects bytes from each line.-c
(characters): Selects characters from each line. This is often preferred for text processing.-d
(delimiter): Specifies a delimiter character to separate fields. The default delimiter is the TAB character.-f
(fields): Selects fields based on the delimiter specified with -d
.Let’s look at some common cut
use cases with illustrative examples. Assume we have a file named data.txt
with the following content:
Name,Age,City
John Doe,30,New York
Jane Smith,25,London
Peter Jones,40,Paris
1. Extracting Specific Characters:
To extract the first three characters of each line:
cut -c 1-3 data.txt
Output:
Nam
Joh
Jan
Pet
To extract the 5th character of each line:
cut -c 5 data.txt
Output:
e
n
e
e
2. Extracting Fields using Delimiter:
To extract the “Name” field (the first field), using a comma as the delimiter:
cut -d ',' -f 1 data.txt
Output:
Name
John Doe
Jane Smith
Peter Jones
To extract the “Age” and “City” fields (second and third fields):
cut -d ',' -f 2,3 data.txt
Output:
Age,City
30,New York
25,London
40,Paris
3. Combining Options:
You can combine options for more complex extractions. For example, to extract the first 10 characters of the second field (Age and City fields) :
cut -d ',' -f 2-3 | cut -c 1-10
Output:
Age,City
30,New York
25,London
40,Paris
4. Using Standard Input:
cut
can also process data from standard input using pipes. For instance, to extract the username from the output of the whoami
command:
whoami | cut -d '_' -f 1
(This example assumes the username is separated by an underscore.)
5. Handling multiple files:
cut
can process multiple files at once. For example, if you have data1.txt
and data2.txt
with similar structures, the following will extract the first field from both:
cut -d ',' -f 1 data1.txt data2.txt
These examples demonstrate the versatility of the cut
command. Experiment with different options and file formats to fully use its capabilities for efficient text processing within your Linux workflow. Remember to always consult the man cut
page for a complete list of options and further details.