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.tsvcut -f <n> -d '<delim>' <file> — Extract the nth field using a custom delimiter.
cut -f 1 -d ',' users.csvcut -f <n>,<m> -d '<delim>' <file> — Extract multiple specific fields.
cut -f 1,3 -d ':' /etc/passwdcut -f <n>-<m> -d '<delim>' <file> — Extract a range of fields.
cut -f 2-4 -d ',' data.csvcut -f <n>- -d '<delim>' <file> — Extract from field n to the end.
cut -f 3- -d ':' /etc/passwdcut -f -<n> -d '<delim>' <file> — Extract from the first field up to field n.
cut -f -3 -d ',' data.csvcut -f <n> -d '<delim>' -s <file> — Suppress lines that do not contain the delimiter.
cut -f 2 -d ',' -s mixed.txtcut -f <n> -d '<delim>' --output-delimiter='<sep>' — Use a different delimiter for the output.
cut -f 1,3 -d ':' --output-delimiter=' | ' /etc/passwdCharacter Extraction
cut -c <n> <file> — Extract the nth character from each line.
cut -c 1 file.txtcut -c <n>-<m> <file> — Extract a range of characters.
cut -c 1-10 file.txtcut -c <n>,<m>,<o> <file> — Extract specific character positions.
cut -c 1,5,10 file.txtcut -c <n>- <file> — Extract from character n to the end of each line.
cut -c 5- file.txtcut -c -<n> <file> — Extract the first n characters of each line.
cut -c -20 file.txtByte Extraction
cut -b <n> <file> — Extract the nth byte from each line.
cut -b 1 file.txtcut -b <n>-<m> <file> — Extract a range of bytes (useful for fixed-width binary data).
cut -b 1-16 binary.datcut -b <n>- <file> — Extract from byte n to the end.
cut -b 5- data.binComplement
cut --complement -f <n> -d '<delim>' <file> — Extract all fields EXCEPT the specified ones.
cut --complement -f 2 -d ',' data.csvcut --complement -c <n>-<m> <file> — Remove a range of characters, keep everything else.
cut --complement -c 1-5 file.txtCommon Patterns
cut -f 1 -d ':' /etc/passwd — Extract all usernames from /etc/passwd.
cut -f 1 -d ':' /etc/passwdecho $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 -nps 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-52rev <file> | cut -f 1 -d '.' | rev — Extract file extensions by reversing, cutting, and reversing again.
ls | rev | cut -f 1 -d '.' | revhead -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
- GNU coreutils: cut – official reference for all options
- cut(1) man page – the Linux manual page for cut