Linux – Using ‘cut’ command efficiently
The cut
command in Linux is a utility for processing and extracting specific portions of lines from files or input streams. It is particularly useful for working with columns of data in text files or command output. Here’s a detailed explanation of how the cut
command works, its options, and practical examples.
1. Basic Syntax
The basic syntax of the cut
command is:
cut [OPTION]... [FILE]...
OPTION
: Flags and arguments specifying how to process the input.FILE
: The file(s) to read from. If not specified,cut
reads from the standard input.
2. Common Options
2.1. -b
(Byte Selection)
The -b
option selects specific bytes from each line.
- Syntax:
cut -b [LIST] [FILE]
LIST
: Specifies the byte positions to be cut. Ranges can be specified using-
, and multiple ranges can be separated by commas.- Example: Extract the first 5 bytes from each line of
file.txt
:
cut -b 1-5 file.txt
- Example: Extract bytes 1, 4, and 7 from each line:
cut -b 1,4,7 file.txt
2.2. -c
(Character Selection)
The -c
option selects specific characters from each line.
- Syntax:
cut -c [LIST] [FILE]
LIST
: Specifies the character positions to be cut. Ranges and multiple values can be specified.- Example: Extract characters 1 through 5 from each line:
cut -c 1-5 file.txt
- Example: Extract characters 1, 4, and 7:
cut -c 1,4,7 file.txt
2.3. -d
(Delimiter Specification)
The -d
option specifies a delimiter to use when extracting fields based on columns.
- Syntax:
cut -d [DELIM] -f [FIELDS] [FILE]
DELIM
: The delimiter separating fields. The default delimiter is the tab character.FIELDS
: Specifies the field numbers to be extracted.- Example: Extract the first and third fields from a comma-separated file:
cut -d ',' -f 1,3 file.txt
- Example: Extract the second field from a tab-separated file (default delimiter):
cut -f 2 file.txt
2.4. -f
(Field Selection)
The -f
option selects specific fields from each line. This option works in conjunction with -d
.
- Syntax:
cut -d [DELIM] -f [FIELDS] [FILE]
FIELDS
: Specifies the field numbers to be cut. Ranges and multiple fields can be specified.- Example: Extract fields 2 through 4 from a file separated by commas:
cut -d ',' -f 2-4 file.txt
- Example: Extract fields 1 and 3:
cut -d ',' -f 1,3 file.txt
2.5. --complement
The --complement
option is used to output the parts of the line that are not selected.
- Syntax:
cut --complement -d [DELIM] -f [FIELDS] [FILE]
- Example: Output all fields except the first field from a comma-separated file:
cut --complement -d ',' -f 1 file.txt
3. Practical Examples
3.1. Extracting Columns from a CSV File
Suppose data.csv
has the following content:
name,age,city
John,30,New York
Alice,25,Los Angeles
Bob,35,Chicago
- Extract names and ages:
cut -d ',' -f 1,2 data.csv
Output:
name,age
John,30
Alice,25
Bob,35
3.2. Extracting Specific Characters
Suppose data.txt
contains:
abcdefgh
ijklmnop
qrstuvwx
- Extract characters 3 through 6:
cut -c 3-6 data.txt
Output:
cdef
lmnop
stuv
3.3. Extracting Byte Positions
Suppose data.txt
contains:
1234567890
abcdefghij
klmnopqrst
- Extract bytes 2 through 5:
cut -b 2-5 data.txt
Output:
2345
bcde
lmn
4. Using cut
with Pipes
The cut
command is often used in conjunction with pipes to process output from other commands.
- Example: List all files in a directory and extract the first 10 characters of each filename:
ls -1 | cut -c 1-10
- Example: Extract the second field from the output of a
ps
command:
ps aux | cut -d ' ' -f 2
5. Summary
The cut
command is a versatile tool for extracting specific portions of text from files or command outputs. Its options for byte, character, and field selection, combined with the ability to specify delimiters and complement selections, make it a powerful utility for processing and manipulating text data. Whether you’re working with structured data files, command outputs, or raw text, cut
can help you extract and format the information you need efficiently.