cut Command Overview
As its name 'cut' suggests, the `cut` command divides each line of text data into several pieces based on a delimiter and outputs only the desired pieces. This command is simple and fast, making it suitable for straightforward data extraction tasks that do not require complex pattern matching.
Key Features of cut
- Extracting fields (columns) separated by a specific delimiter
- Extracting a specific byte range from a line
- Extracting a specific character range from a line
- Capable of processing both standard input and files
Key Options
You can precisely cut data using the core options of the `cut` command.
1. Field-based Extraction
2. Byte and Character-based Extraction
Generated command:
Try combining the commands.
Description:
`cut` Executes the command.
Combine the above options to virtually execute commands with AI.
Commonly Used Examples
Explore various practical examples using the `cut` command to learn data extraction methods.
Extract Specific Columns from a Space-Delimited File
cat /etc/passwd | cut -d: -f1
Extracts only the username (first field) from the `passwd` file. If the `-d` option is not specified, the default delimiter is tab, but since the `passwd` file uses `:` as a delimiter, you must specify `: -f1`.
Extract Name and Email from a CSV File
cut -d',' -f1,3 users.csv
Extracts the name (first field) and email (third field) from the `users.csv` file, which is delimited by commas (,).
Extract Specific Character Range from a File
cut -c1-5 data.txt
Cuts out only from the first character to the fifth character of each line in the `data.txt` file.
Extract IP Address Using Pipes
ifconfig eth0 | grep 'inet ' | cut -d: -f2 | cut -d' ' -f1
Passes the output of the `ifconfig` command through pipes to `grep` and `cut` to extract only the IP address. (Example: `ifconfig | grep 'inet ' | cut -d ' ' -f2`)
Output Excluding Specific Fields
cut -d: -f1,6 --complement /etc/passwd
Outputs all information from the `passwd` file except for the username (field 1) and home directory (field 6).