cut

The cut command in Unix/Linux is used to extract sections from each line of a file or input stream. It is particularly useful for working with delimited text files, where fields are separated by a specified delimiter character.

cut [options] [file]
Here are some common options:
    -f or --fields:     Specify the fields to extract. You can provide a comma-separated list of field numbers or a range (e.g., -f 1,3 or -f 1-3).
    -d or --delimiter:  Specify the field delimiter character. The default delimiter is the tab character.
    -c or --characters: Specify the characters to extract. This can be a comma-separated list of character positions or a range (e.g., -c 1,3 or -c 1-3).

Examples

Cut by delimiter and print specific fields
cut -d',' -f1,3 filename.csv
Cut by tab delimiter and print a range of characters
cut -f2-5 -d$'\t' filename.tsv
Cut by space delimiter and print specific field
cut -d' ' -f2 filename.txt
Cut by comma delimiter and print from character 3 to end
cut -d',' -c3- filename.csv
Cut by colon delimiter and print specific field
cut -d':' -f2 filename.txt
Cut by whitespace delimiter and print specific fields
cut -d' ' -f1,3 filename.txt
Cut by hyphen delimiter and print from character 2 to end
echo "123-456-789" | cut -d'-' -c2-
Cut by underscore delimiter and print last field
cut -d'_' -f3 filename.txt
Cut by comma delimiter and print specific characters
cut -d',' -c1,3,5 filename.csv
Cut by colon delimiter and print from character 1 to 5
cut -d':' -c1-5 filename.txt
Cut by space delimiter and print specific range of characters
cut -d' ' -c3-8 filename.txt
Cut by tab delimiter and print specific fields
cut -f2,4,6 -d$'\t' filename.tsv
Cut by comma delimiter and print specific characters
cut -d',' -c2,5-7 filename.csv
Cut by hyphen delimiter and print specific fields
cut -d'-' -f1,3 filename.txt
Cut by colon delimiter and print specific range of characters
cut -d':' -c2-8 filename.txt
Cut by space delimiter and print last field
cut -d' ' -f6 filename.txt
Cut by comma delimiter and print specific fields
cut -d',' -f2,4 filename.csv
Cut by tab delimiter and print specific range of characters
cut -f2-5 -d$'\t' filename.tsv
Cut by comma delimiter and print from character 2 to 6
cut -d',' -c2-6 filename.csv
Cut by space delimiter and print specific range of characters
cut -d' ' -c4-10 filename.txt