Linux

cut

Remove sections from each line of files.

#linux #text-processing

Cut by Character

Single Character

Extract 5th character:

cut -c 5 [file]

Character Range

Extract characters 1-10:

cut -c 1-10 [file]

From Character to End

Extract from character 5 to end:

cut -c 5- [file]

Up to Character

Extract up to character 10:

cut -c -10 [file]

Multiple Ranges

Extract characters 1-5 and 10-15:

cut -c 1-5,10-15 [file]

Cut by Field

By Tab (Default)

Extract first field:

cut -f 1 [file]

Multiple Fields

Extract fields 1 and 3:

cut -f 1,3 [file]

Field Range

Extract fields 2-5:

cut -f 2-5 [file]

From Field to End

Extract from field 3 to end:

cut -f 3- [file]

Custom Delimiter

Comma Delimiter (CSV)

cut -d ',' -f 1,2 [file]

Colon Delimiter

cut -d ':' -f 1,3 /etc/passwd

Space Delimiter

cut -d ' ' -f 1,3 [file]

Output Delimiter

Change Output Delimiter

Input is comma, output is tab:

cut -d ',' -f 1,2 --output-delimiter=$'\t' [file]

Custom output delimiter:

cut -d ',' -f 1,2,3 --output-delimiter=' | ' [file]

Complement

Exclude Fields

Exclude field 2 (print all except field 2):

cut -f 2 --complement [file]

Common Use Cases

Extract Username from /etc/passwd

cut -d ':' -f 1 /etc/passwd

Extract Email Domain

cut -d '@' -f 2 emails.txt

Extract IP Addresses from Log

cut -d ' ' -f 1 access.log

Get First Column of CSV

cut -d ',' -f 1 data.csv

Examples with Pipes

With grep

grep "pattern" [file] | cut -d ',' -f 1,3

With sort

cut -d ',' -f 2 [file] | sort

With uniq

cut -d ' ' -f 1 [file] | sort | uniq

Tips

  • Use -f for fields (columns)
  • Use -c for characters (positions)
  • Default delimiter is TAB
  • Can't use space as default delimiter (must specify -d ' ')
  • For complex delimiters, use awk instead

Comparison with awk

cut is simpler but less flexible than awk:

Select fields with cut.

cut -d ',' -f 1,3 file.csv

Use awk for the equivalent result.

awk -F',' '{print $1, $3}' file.csv