Skip to Content
gnux

cutx

Remove sections from each line of files.

Mirrors GNU cut. Run it as cutx, or as gnux cutx.

Synopsis

cutx OPTION... [FILE]...

GNU reference

Description

cutx prints selected parts of each line of each FILE to standard output. With no FILE, or when FILE is -, it reads standard input. Exactly one of -b (bytes), -c (characters) or -f (fields) must be given, with a LIST of positions: N, N- (from N to the end of the line), N-M, or -M (from the first to M), separated by commas or blanks. Positions are numbered from 1, and the selection is printed in input order whatever the order of the LIST, each position at most once. With -f the line is split on TAB, or on the single character given with -d (-d '' means NUL); a line with no delimiter is printed whole, unless -s suppresses it. --complement selects everything the LIST does not. --output-delimiter=STRING is written between the selected fields (the input delimiter by default), and with -b or -c between non-overlapping ranges. -z makes NUL the line delimiter. Every output line ends with the line delimiter. As in GNU cut, -c counts bytes, not multibyte characters, so -c and -b are the same, and -n is accepted and ignored. One difference from GNU: giving the same kind of list twice (-f1 -f2) keeps the last one instead of refusing.

Permissions

Needs read permission on each FILE and search permission on the directories leading to it. Nothing is ever modified.

Use cases

  • Extracting columns from CSV, TSV or colon-separated files such as /etc/passwd.
  • Taking a fixed-width slice of every line, such as a date prefix of a log.
  • Dropping a column with --complement.
  • Turning a delimited file into another delimiter with --output-delimiter.
  • Skipping header or comment lines that have no delimiter with -s.
  • Pulling one value out of each line of a command's output in a script.
  • Splitting NUL-separated records safely with -z.

Flags

  -b, --bytes string              select only these bytes
  -c, --characters string         select only these characters
      --complement                complement the set of selected bytes, characters or fields
  -d, --delimiter DELIM           use DELIM instead of TAB for field delimiter
  -f, --fields string             select only these fields; also print any line that contains no delimiter character, unless the -s option is specified
      --help                      display this help and exit
  -s, --only-delimited            do not print lines not containing delimiters
      --output-delimiter STRING   use STRING as the output delimiter; the default is to use the input delimiter
      --version                   output version information and exit
  -z, --zero-terminated           line delimiter is NUL, not newline

Examples

cutx -f 1 data.tsv
cutx -d : -f 1,7 /etc/passwd
cutx -d , -f 2-3 people.csv
cutx -d , -f 3- people.csv

Drop the second column, keeping the rest

cutx -d , -f 2 --complement people.csv
cutx -b 1-10 app.log
cutx -c 5-8 codes.txt

Convert a CSV file to TAB-separated output

cutx -d , -f 1- --output-delimiter=$'\t' people.csv

Join the selected columns with a custom separator

cutx -d : -f 1,3 --output-delimiter=' -> ' passwd.txt

Skip lines that contain no delimiter, such as comments

cutx -s -d = -f 2 settings.ini

Pull one field out of a command's output

echox 'key=value' | cutx -d = -f 2

Process NUL-separated records

cutx -z -d / -f 1 paths.lst

Count the distinct values of a column in the portable gnux shell

gnux shell -c 'cutx -d , -f 1 people.csv | uniqx -c'

Options

Each option has a page of its own, with its usage and examples.

Exit status

StatusMeaning
0success: every operand was processed
1failure: an operand could not be processed (diagnosed on standard error), or a command-line mistake

Help in the terminal

cutx --help                # options, first examples, exit status
gnux help cutx             # this page: the full manual
gnux help cutx --OPTION    # one option in depth, with its examples

See also

On this page