[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]

Commands Reference, Volume 1


cut Command

Purpose

Writes out selected bytes, characters, or fields from each line of a file.

Syntax

cut-b List -n ] |  -c List -f List -s ] [  -d Character ] } [ File ... ]

Description

The cut command cuts bytes, characters, or fields from each line of a file and writes these bytes, characters, or fields to standard output. If you do not specify the File parameter, the cut command reads standard input.

You must specify either the -b, -c, or -f flag. The List parameter is a comma-separated, blank-separated, or hyphen-separated list of integer numbers (in increasing order). The hyphen separator indicates ranges. The following entries are some example List parameters which could refer to bytes, characters, or fields:

1,4,7
1-3,8
-5,10
3-

where -5 is a short form for the first through fifth and 3- is a short form for the third through last.

If using the cut command on fields, the length of the fields specified by the List parameter can vary from field to field and line to line. The position of the field delimiter character, such as a tab character, determines the length of a field.

You can also use the grep command to make horizontal cuts through a file and the paste command to put the files back together. To change the order of columns in a file, use the cut and paste commands.

Flags


-b List Specifies byte positions. These byte positions ignore multibyte character boundaries unless the -n flag is also specified.
-c List Specifies character positions. For example, if you specify -c 1-72, the cut command writes out the first 72 characters in each line of the file.
-d Character Uses the character specified by the Character variable as the field delimiter when you specify the -f flag. You must put quotation marks around characters with special meaning to the shell, such as the space character.
-f List Specifies a list of fields assumed to be separated in the file by a delimiter character, which is by default the tab character. For example, if you specify -f 1,7, the cut command writes out only the first and seventh fields of each line. If a line contains no field delimiters, the cut command passes them through intact (useful for table subheadings), unless you specify the -s flag.
-n Suppresses splitting of multibyte characters. Use only with the -b flag. If the last byte of a character falls within the range denoted by the List variable of the -b flag, the character is written; otherwise, the character is excluded.
-s Suppresses lines that do not contain delimiter characters. Use only with the -f flag.

Exit Status

This command returns the following exit values:

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

Examples

  1. To display several fields of each line of a file, enter:
    cut  -f 1,5 -d : /etc/passwd
    This displays the login name and full user name fields of the system password file. These are the first and fifth fields (-f 1,5) separated by colons (-d :).

    For example, if the /etc/passwd file looks like this:

    su:*:0:0:User with special privileges:/:/usr/bin/sh
    daemon:*:1:1::/etc:
    bin:*:2:2::/usr/bin:
    sys:*:3:3::/usr/src:
    adm:*:4:4:System Administrator:/var/adm:/usr/bin/sh
    pierre:*:200:200:Pierre Harper:/home/pierre:/usr/bin/sh
    joan:*:202:200:Joan Brown:/home/joan:/usr/bin/sh
    

    The cut command produces:

    su:User with special privileges
    daemon:
    bin:
    sys:
    adm:System Administrator
    pierre:Pierre Harper
    joan:Joan Brown
    
  2. To display fields using a blank separated list, enter:

    cut -f "1 2 3" -d : /etc/passwd
    

    The cut command produces:

    su:*:0
    daemon:*:1
    bin:*:2
    sys:*:3
    adm:*:4
    pierre:*:200
    joan:*:202
    

Files


/usr/bin/cut Contains the cut command.

Related Information

The grep command, paste command, sh command.

Files Overview in AIX 5L Version 5.1 System User's Guide: Operating System and Devices describes what files are and how they are stored by the operating system.

Input and Output Redirection Overview in AIX 5L Version 5.1 System User's Guide: Operating System and Devices describes how to redirect input and output.


[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]