cut Command Guide
The cut command extracts portions of each line from files. Learn how to select columns, fields, and character ranges.
5 min read•Last updated: 2024
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Fields
cut -f 1 fileFirst field (tab sep)cut -f 1,3 fileFields 1 and 3cut -f 1-3 fileFields 1 to 3Delimiter
-d ":"Use : as delimiter-d ","Use , (CSV)-d " "Use spaceCharacters
-c 1-5Chars 1 to 5-c 1,5,10Specific chars-c 5-From char 5 to endCommon
cut -d: -f1 /etc/passwdGet usernamescut -d, -f2 file.csvCSV column 2cut --complement -f1All except field 1Downloadable Image Preview
Failed to generate preview
Basic Usage
cut extracts specific portions from each line of input.
bash
# Cut by field (default delimiter is tab)
cut -f1 file.txt
# Cut by character position
cut -c1-10 file.txtCommon Options
cut Options
| -f LIST | Select fields (columns) |
| -d CHAR | Use CHAR as field delimiter |
| -c LIST | Select characters |
| -b LIST | Select bytes |
| --complement | Select everything except specified |
| -s | Only print lines with delimiter |
Selecting Fields
bash
# Single field (tab-delimited)
cut -f2 file.txt
# Multiple fields
cut -f1,3 file.txt
# Range of fields
cut -f2-4 file.txt
# Field 2 to end
cut -f2- file.txtCustom Delimiter
bash
# CSV (comma-separated)
cut -d',' -f1,2 data.csv
# Colon-separated (like /etc/passwd)
cut -d':' -f1 /etc/passwd
# Space-separated
cut -d' ' -f2 file.txtTip
For complex CSV files with quoted fields, consider using
awk or specialized tools like csvcut.Selecting Characters
bash
# First 5 characters
cut -c1-5 file.txt
# Character 5 to end
cut -c5- file.txt
# Specific characters
cut -c1,3,5 file.txt
# First 3 and last 3 characters
cut -c1-3,-3 file.txtComplement Selection
bash
# All fields EXCEPT field 2
cut -f2 --complement data.csv
# All characters EXCEPT first 5
cut -c1-5 --complement file.txtPractical Examples
Extract usernames from /etc/passwd
bash
cut -d':' -f1 /etc/passwdGet specific columns from CSV
bash
cut -d',' -f1,3,5 data.csvExtract IP addresses from log
bash
cut -d' ' -f1 access.logGet file extensions
bash
ls | cut -d'.' -f2Extract date from timestamp
bash
echo "2024-01-15 10:30:00" | cut -d' ' -f1Remove first column
bash
cut -f1 --complement data.txtExtract domain from URLs
bash
echo "https://example.com/path" | cut -d'/' -f3Process PATH variable
bash
echo $PATH | cut -d':' -f1-3cut vs awk
awk is more powerful for complex field extraction:
bash
# cut: simple, fast, single delimiter
cut -d',' -f2 data.csv
# awk: handles multiple delimiters, calculations
awk -F',' '{print $2}' data.csv
# awk: can handle variable whitespace
awk '{print $2}' file.txtInfo
cut treats consecutive delimiters as separate fields. Use
awk if your data has variable whitespace between fields.Output Delimiter
bash
# Change output delimiter
cut -d':' -f1,3 --output-delimiter=',' /etc/passwdSummary
cut is efficient for simple field extraction. Key takeaways:
- Use
cut -f Nfor field extraction - Use
cut -dto set delimiter - Use
cut -cfor character positions - Use
--complementto exclude selections - Consider
awkfor complex cases