Previous Table of Contents Next

Module 29
cut (SV)


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:

*  Displays columns of a table or file
*  Displays fields of a table or file
*  A field delimiter may be specified
*  Suppress lines with no line delimiters

BSD (Berkeley)
Although cut is not usually implemented on BSD systems, you can accomplish the same results using nawk.


Following is the general format of the cut command.

     cut -clist [ file_list ]
     cut -flist [ -dchar ] [ -s ] [ file_list ]


The following list describes the options and their arguments that may be used to control how cut functions.

-clist 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.
-flist 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.
-dchar 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.


The following argument may be passed to the cut command.

file_list One or more files to be cut and displayed.


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.


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.


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.


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.

1.  Cut the first field of the db/phone file into the first file by typing cut -f1 -d: db/phone > first. The -d: specifies that the fields are colon (:) separated.
2.  If you are using the ksh, press Escape, type kf1r4ffcwcity, and press Return. If you do not have the ksh, type cut -f4 -d: db/phone > city and press Return.
3.  Type cat first city and press Return to display the two new files.
4.  Type who -u | cut -c-8,43- to display the usernames of all users currently logged on the system and the PID for their login shell. This assumes your who command displays the standard who information.
5.  Turn to Module 101 to continue the learning sequence.

Previous Table of Contents Next