[Next] [Previous] [Up] [Top] [Contents]

8.1 Working With Files

8.1.3 cut - select parts of a line

The cut command allows a portion of a file to be extracted for another use.

Syntax

cut [options] file

Common Options

-c character_list character positions to select (first character is 1)

-d delimiter field delimiter (defaults to <TAB>)

-f field_list fields to select (first field is 1)

Both the character and field lists may contain comma-separated or blank-character-separated numbers (in increasing order), and may contain a hyphen (-) to indicate a range. Any numbers missing at either before (e.g. -5) or after (e.g. 5-) the hyphen indicates the full range starting with the first, or ending with the last character or field, respectively. Blank-character-separated lists must be enclosed in quotes. The field delimiter should be enclosed in quotes if it has special meaning to the shell, e.g. when specifying a <space> or <TAB> character.

Examples

In these examples we will use the file users:

jdoe John Doe 4/15/96

lsmith Laura Smith 3/12/96

pchen Paul Chen 1/5/96

jhsu Jake Hsu 4/17/96

sphilip Sue Phillip 4/2/96

If you only wanted the username and the user's real name, the cut command could be used to get only that information:

% cut -f 1,2 users

jdoe John Doe

lsmith Laura Smith

pchen Paul Chen

jhsu Jake Hsu

sphilip Sue Phillip

The cut command can also be used with other options. The -c option allows characters to be the selected cut. To select the first 4 characters:

% cut -c 1-4 users

This yields:

jdoe

lsmi

pche

jhsu

sphi

thus cutting out only the first 4 characters of each line.


Introduction to Unix - 14 AUG 1996
[Next] [Previous] [Up] [Top] [Contents]