cut: extract columns from text
cut prints selected parts of each line: fields separated by a delimiter, or character positions. It is simpler and faster than awk when the input has a single, consistent separator.
cut -d DELIM -f FIELDS [file ...]
cut -c CHARACTERS [file ...]
Examples
cut -d: -f1 /etc/passwd # user names
cut -d: -f1,6 /etc/passwd # user and home directory
cut -d, -f2-4 data.csv # columns 2 to 4
cut -d, -f3- data.csv # column 3 to the end
cut -c1-10 file.txt # first 10 characters of each line (ASCII text, see below)
cut -f2 table.tsv # tab is the default delimiter
cut -d' ' -f1 access.log | sort | uniq -c | sort -rn | head # top client IPs
cut -d: -f1 --complement /etc/passwd # everything except field 1 (GNU)
cut -d: -f1,7 --output-delimiter=' ' /etc/passwd # GNU
Limits
- The delimiter is one character. Several spaces in a row count as several empty fields, so
cut -d' 'fails on aligned output such aspsordf. Useawk '{print $2}', or squeeze spaces first:tr -s ' ' | cut -d' ' -f2. - Fields are printed in their original order:
-f3,1gives the same as-f1,3. Use awk to reorder. - Quoted CSV fields with commas inside are not handled.
- GNU cut treats
-clike-band counts bytes, so it can split accented and other multibyte UTF-8 characters. Useawk '{print substr($0,1,10)}'with GNU awk in a UTF-8 locale for those.
Related: paste and tr
paste -d, names.txt emails.txt # join files line by line, side by side
paste -sd+ numbers.txt | bc # sum a column of numbers
tr 'a-z' 'A-Z' < file.txt # upper case
tr -d '\r' < windows.txt > unix.txt # remove Windows carriage returns
tr -s ' ' < file.txt # squeeze repeated spaces