Home - Resumé - Up


Name

field - extract selected fields from each line

Synopsis

field [−abem] [−d delims] [−o delims] [−f fields] [file ...]
field [−abem] [−d delims] [−o delims] fields [file ...]

Description

The field filter writes parts of each input line selected by a list of fields.

A list of fields is a comma-separated list of field numbers or field ranges. A field range consists of an optional starting field number, a dash, and an optional ending field number. If the starting field number is omitted, then the range begins with the first field; if the ending field number is omitted, then the range ends with the last field on the input line. Input fields are numbered beginning with 1; a field number of 0 produces an empty field in the output.

The options are as follows:

−a
Require all fields in the list to be present (though they may be empty). Field skips lines that do not contain all the listed fields.

−b
Trim blanks from the beginning and end of each field.

−d delims
Fields are separated by any character from delims. Without −d, fields are separated by sequences of white space. (Following sort(1) , field accepts −t as a synonym for −d.)

−e
Write a line for every line in the input file. Normally, field skips a line for which it writes no fields.

−f fields
Select the specified fields; see below. Without a −f option, field takes the first non-option argument to be the field list.

−m
Write all field positions in the fields list. Normally field skips any field positions beyond the last field on the actual input line.

−o delims
Write fields separated by characters from delims. Each character is used in turn; if delims contains fewer characters than necessary, then field uses the final one for all remaining separators. It is possible to specify an empty string, in which case field writes nothing between output fields. Without a −o option, field field uses the first character from the input delimiters, or failing that, a tab character.

Field Vs Cut

The cut(1) utility, introduced in System III UNIX, performs a somewhat similar function. The major differences are, first, that field by default considers each line to consist of fields separated by sequences of white space, while cut can only split on every instance of a single delimiter character; second, that field currently operates only on delimited fields, and not on character or byte ranges; and third, that field writes input fields in the order given by its fields list, and thus can be used to reorder or repeat input fields.

Examples:

ls -l | field 9,5
field -a -d: 1,5 /etc/passwd

See Also

cut(1) , sort(1)