Previous | Table of Contents | Next |
DESCRIPTION
The external cut command displays selected columns or fields from each line of a file. A column is one character position. A field is a delimiter (tab) separated group of characters . The following functions and features are provided by cut :
|
---|
BSD (Berkeley) |
Although cut is not usually implemented on BSD systems, you can accomplish the same results using nawk . |
|
COMMAND FORMAT
Following is the general format of the cut command.
cut -clist [ file_list ] cut -flist [ -dchar ] [ -s ] [ file_list ]
Options
The following list describes the options and their arguments that may be used to control how cut functions.
-c list | Display (cut) columns, specified in list , from the input data. No spaces are allowed within the list. Multiple values must be comma (,) separated. The list defines the exact columns to display. For example, the -c1,4,7 notation displays columns 1, 4, and 7 of the input. The -c-10,50- format would display columns 1 through 10 and 50 through end-of-line. |
-f list | Display (cut) fields, specified in list , from the input data. No spaces are allowed within the list. Multiple values must be comma (,) separated. The list defines the exact field to display. For example, -f1,4,7 would display fields 1, 4, and 7. The -f2,4-6,8 would display fields 2, 4, 5, 6, and 8. |
-d char | The character char is used as the field delimiter. The default delimiter is a tab character. To use a character that has special meaning to the shell, you must quote the character so the shell does not interpret it. For example, to use a single space as a delimiter, type -d . |
-s | Suppress lines that do not contain a field delimiter. Normally these lines are displayed. |
Arguments
The following argument may be passed to the cut command.
file_list | One or more files to be cut and displayed. |
DIAGNOSTICS AND BUGS
The following messages may be returned from cut if a problem is encountered .
line too long | A line in the file is longer than 1023 characters or fields. May also be caused from no new-line characters in the file. |
bad list for c/f option | Either the list for the c or f option is incorrect or no c or f option was specified. |
no fields | No list was specified for the c or f option. |
no delimiter | No char was specified for the d option. |
cannot handle multiple adjacent backspaces | Multiple backspaces located beside each other cannot be handled correctly by cut . |
cannot open file | Either you do not have read permission to the specified file or the file does not exist. |
RELATED COMMANDS
Refer to the grep command described in Module 60 on selecting rows from a file. Another database type command is join discussed in Module 69. Other related commands include the paste and pr commands discussed in modules 101 and 105.
APPLICATIONS
You can use the cut command to project data from a file. That is you can select certain fields or columns and display them. By using shell indirection you can create new files with a subset of columns or fields contained in the first file.
Sometimes cut is used in shell programming to select certain values from a line. This is one of many ways to perform such a selection. If you are selecting fields for a value, you should probably use the set command where possible and echo the desired positional parameter. Whereas, you may have to use the cut command to select values from exact columns.
TYPICAL OPERATION
In this activity you use the cut command to display fields out of the /etc/passwd file and cut columns from the output of the who command. Begin at the shell prompt.
Previous | Table of Contents | Next |