cut — Extract Fields, Characters and Bytes from Text Lines

Practical guide to cut — extract specific fields, characters or byte ranges from text files and piped output, with delimiters, ranges and complement.

cut is the lean tool for slicing exactly the column you need out of every line – whether that is a field in a CSV file, a character range in a log line, or a byte offset in fixed-width data. You tell it whether to cut by fields (-f), characters (-c) or bytes (-b), and cut returns the rest line by line. By default it splits fields on the tab character; with -d you set your own delimiter. In pipelines, cut is often the fastest answer when awk would be overkill.

Field Extraction

cut -f <n> <file> — Extract the nth field (tab-delimited by default).

cut -f 2 data.tsv

cut -f <n> -d '<delim>' <file> — Extract the nth field using a custom delimiter.

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

cut -f <n>,<m> -d '<delim>' <file> — Extract multiple specific fields.

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

cut -f <n>-<m> -d '<delim>' <file> — Extract a range of fields.

cut -f 2-4 -d ',' data.csv

cut -f <n>- -d '<delim>' <file> — Extract from field n to the end.

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

cut -f -<n> -d '<delim>' <file> — Extract from the first field up to field n.

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

cut -f <n> -d '<delim>' -s <file> — Suppress lines that do not contain the delimiter.

cut -f 2 -d ',' -s mixed.txt

cut -f <n> -d '<delim>' --output-delimiter='<sep>' — Use a different delimiter for the output.

cut -f 1,3 -d ':' --output-delimiter=' | ' /etc/passwd

Character Extraction

cut -c <n> <file> — Extract the nth character from each line.

cut -c 1 file.txt

cut -c <n>-<m> <file> — Extract a range of characters.

cut -c 1-10 file.txt

cut -c <n>,<m>,<o> <file> — Extract specific character positions.

cut -c 1,5,10 file.txt

cut -c <n>- <file> — Extract from character n to the end of each line.

cut -c 5- file.txt

cut -c -<n> <file> — Extract the first n characters of each line.

cut -c -20 file.txt

Byte Extraction

cut -b <n> <file> — Extract the nth byte from each line.

cut -b 1 file.txt

cut -b <n>-<m> <file> — Extract a range of bytes (useful for fixed-width binary data).

cut -b 1-16 binary.dat

cut -b <n>- <file> — Extract from byte n to the end.

cut -b 5- data.bin

Complement

cut --complement -f <n> -d '<delim>' <file> — Extract all fields EXCEPT the specified ones.

cut --complement -f 2 -d ',' data.csv

cut --complement -c <n>-<m> <file> — Remove a range of characters, keep everything else.

cut --complement -c 1-5 file.txt

Common Patterns

cut -f 1 -d ':' /etc/passwd — Extract all usernames from /etc/passwd.

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

echo $PATH | cut -f 1 -d ':' — Get the first directory in the PATH variable.

echo $PATH | cut -f 1 -d ':'

cut -f 1,3 -d ':' /etc/passwd | sort — Extract username and UID, then sort.

cut -f 1,3 -d ':' /etc/passwd | sort -t ':' -k 2 -n

ps aux | cut -c 1-80 — Truncate process listing to 80 characters wide.

ps aux | cut -c 1-80

<command> | cut -f <n> -d ' ' — Extract a column from space-delimited command output.

df -h | cut -c 1-20,45-52

rev <file> | cut -f 1 -d '.' | rev — Extract file extensions by reversing, cutting, and reversing again.

ls | rev | cut -f 1 -d '.' | rev

head -1 data.csv | cut -f 1- -d ',' --output-delimiter=$'\n' — List CSV column headers one per line.

head -1 data.csv | cut -f 1- -d ',' --output-delimiter=$'\n'

Conclusion

cut wins through simplicity: when your data has a clear, consistent delimiter, you extract the column you want faster and more readably than with any other tool. Mind its limits, though – cut knows only a single-character delimiter (it cannot treat repeated spaces as one, the way awk does) and cannot reorder fields. For multiple consecutive spaces, squeeze them first with tr -s ' '; as soon as logic, conditions or variable separators come into play, awk is the better choice. With multibyte (UTF-8) characters, -c and -b differ – cut by character, not by byte, in that case.

Further Reading

  • awk – powerful text processing with fields, conditions and arithmetic
  • paste – merge lines of several files column by column
  • column – format input into clean, aligned columns and tables