The cut command
The cut command prints chosen columns from each line: cut -d…
The cut command prints chosen columns from each line: cut -d, -f2 file.csv prints the second comma-separated field, and cut -c1-5 the first five characters.
Taught in Intermediate, lesson 21
A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.
Usage
cut -d DELIM -f LIST [FILE]... cut -c LIST [FILE]...
Print selected columns or characters from each line. Prints part of every line. With -f it splits each line at a delimiter (a tab unless you give -d) and prints the fields you list; with -c it prints characters by position. A LIST is numbers and ranges separated by commas: 2, 1,3, 2-4, 3- (from 3 to the end) or -2 (up to 2). Fields always come out in their original order.
How to use cut
One column of a CSV file
-d, says fields are separated by commas and -f2 picks the second one.
cut -d, -f2 data.csvscore 82 91 77
Several columns
Use commas and ranges: -f1,3, -f2-4 or -f3- (from 3 to the end).
cut -d: -f1,6 /etc/passwdroot:/root www-data:/var/www learner:/home/learner
Characters by position: cut -c
cut -c1-5 app.log08:00 08:05 08:06 08:07 08:10 08:15
Split on spaces
cut uses exactly one character as the delimiter, so several spaces in a row make empty fields. For columns lined up with spaces, awk is easier.
cut -d' ' -f2 app.log | sort | uniq -c 1 ERROR
1 error:
3 INFO
1 WARN
Options
| -f LIST, --fields=LIST | print these fields (columns) |
|---|---|
| -d DELIM, --delimiter=DELIM | fields are separated by DELIM (one character) instead of a tab |
| -c LIST, --characters=LIST | print these characters |
| -s, --only-delimited | with -f, skip lines that have no delimiter |
| --complement | print everything except the fields or characters listed |
| --output-delimiter=TEXT | join the fields with TEXT instead of the delimiter |
More examples
cut -d, -f1 data.csvname Ava Leo Mia
cut -d, -f2 data.csv | tail -n +282 91 77
cut -d: -f1 /etc/passwdroot www-data learner
Questions about cut
What is the default delimiter for cut?
A tab. Give another one with -d, such as -d, for CSV or -d: for /etc/passwd.
How do I change the order of columns?
cut always prints fields in their original order, even if you write -f2,1. Use awk instead: awk -F, '{print $2 "," $1}' file.csv.
Learn it step by step
Related commands
Every everyday command on one page, to print or keep open: the Linux commands cheat sheet.