The cut Command

Data comes in columns. CSV files, log entries, config files - all have structure.

cut extracts specific columns from text.

Cut by Delimiter

Most useful: split by a character and pick fields:

Terminal
$echo 'john:x:1000:1000:John:/home/john:/bin/bash' | cut -d':' -f1
john
$echo 'john:x:1000:1000:John:/home/john:/bin/bash' | cut -d':' -f6
/home/john
  • -d':' = delimiter is :
  • -f1 = first field
  • -f6 = sixth field

Multiple Fields

Terminal
$cat /etc/passwd | cut -d':' -f1,3
root:0 user:1000 nobody:65534
$cat /etc/passwd | cut -d':' -f1-3
root:x:0 user:x:1000 nobody:x:65534
  • -f1,3 = fields 1 and 3
  • -f1-3 = fields 1 through 3

CSV Processing

Terminal
$cat data.csv
name,email,department john,john@example.com,engineering jane,jane@example.com,marketing
$cut -d',' -f2 data.csv
email john@example.com jane@example.com

Quoted CSV

cut doesn't handle quoted fields properly. For proper CSV with quotes, use awk or a dedicated CSV tool.

Cut by Character Position

Terminal
$echo 'Hello World' | cut -c1-5
Hello
$echo '20240114' | cut -c1-4,5-6,7-8 --output-delimiter='-'
2024-01-14
  • -c1-5 = characters 1 through 5
  • -c1,3,5 = characters 1, 3, and 5

Real-World Examples

Extract Usernames from /etc/passwd

Terminal
$cut -d':' -f1 /etc/passwd | head
root daemon bin sys user

Get Column from Log File

Terminal
$cat access.log | cut -d' ' -f1
(IP addresses)

Parse PATH Variable

Terminal
$echo $PATH | tr ':' '\n'
/usr/local/bin /usr/bin /bin

(Here tr is better since PATH uses : but we want each path on its own line.)

The Default Delimiter

Without -d, cut uses TAB as the delimiter:

Terminal
$echo -e 'a\tb\tc' | cut -f2
b

Complement (Everything Except)

Terminal
$echo 'a:b:c:d:e' | cut -d':' -f2
b
$echo 'a:b:c:d:e' | cut -d':' -f2 --complement
a:c:d:e

--complement gives you everything except the specified fields.

Knowledge Check

How do you extract the third field from a colon-separated file?

Key Takeaways

  • -d sets the delimiter
  • -f selects fields (1-indexed)
  • -c selects character positions
  • Use -f1,3,5 for specific fields
  • Use -f1-5 for ranges
  • Default delimiter is TAB
  • cut is simple but doesn't handle complex CSV

Next: sorting lines with sort.