[Previous] [Contents] [Next]

cut

Cut out selected fields of each line of a file (POSIX)

Syntax:

cut -c list [file...]

cut -f list [-d delim] [-s] [file...]

Options:

-c list
Cut out the characters found in the character positions specified by list. For example, a list of -c 1-64 would output the first 64 characters of each line.
-d delim
Use the delimiter specified by delim (default is tab).
-f list
Cut out the fields specified by list. For example, a list of -f 2,9 would output the second and ninth fields. The fields described by list are assumed to be separated in the file by a delimiter character (see option -d). Lines without field delimiters are passed through intact, unless -s is specified.
-s
If -f is specified, suppress lines with no field delimiters.
file
The pathname of a text file, whose contents are used instead of the standard input.

Description:

For every file you name, the cut utility cuts out columns or fields from each line, concatenates them, and writes them to the standard output.

If the fields are of a fixed length, they can be selected by character position with option -c. If, however, the fields vary in length from line to line, they can be selected with option -f, provided they are separated by a delimiter character. By default, cut assumes the field delimiter character to be tab. You can specify another delimiter through option -d.

In options -c and -f, list is a comma-separated list of integers (in increasing order), with an optional dash (-) to indicate ranges.

You can use the cut utility as a filter; if no files are given, the standard input is used.

Examples:

The following are examples of the list argument:

list argument: Meaning:
1,4,7 Select the first, fourth, and seventh characters or fields.
1-3,8 Equivalent to 1, 2, 3, 8.
-5,10 Equivalent to 1, 2, 3, 4, 5, 10.
3- Equivalent to the third through last.

Map userids to names:

    cut -d: -f1,5 /etc/passwd

List filenames and their permissions:

    ls -l | cut -c57-79,56,56,1-11

Files:

The input files are text files.

Exit status:

0
All input files were output successfully.
>0
An error occurred.

See also:

awk, grep, join, paste, sed


[Previous] [Contents] [Next]