Cut is a Unix command-line tool used to extract fields and the like from lines of input, available on many platforms.

Options edit

Command-line options aka switches of cut:

  • -clist: Display selected characters of lines.
  • -flist: Display selected fields of lines, separated by tab by default.
  • -dchar: Switch field separator from tab to char.
  • -s: Do not display ("suppress") lines that contain no occurrence of the field separator.

Command-line options aka switches of some versions of cut:

  • -b: Display selected bytes of lines.
  • -n: Do not split multi-byte characters.

Examples edit

Examples of cut use:

  • cut -f1 file.txt
    • Displays the 1st field of each line, using tab as the field separator.
  • echo a:b | cut -d: -f2
    • Displays the 2nd field of each line, using colon as the field separator.
  • echo a b c | cut -d" " -f1,3
  • echo a b c d e | cut -d" " -f1-3,5
  • echo a b c | cut -d" " -f3,2,1
    • Outputs "a b c", disregarding the reversed order after -f.
  • echo a b c d | cut -d" " -f2-
    • Outputs the 2nd and every later field, so "b c d".
  • echo abcd | cut -c3,4
    • Instead of fields, treats characters. Thus, outputs "cd".
  • echo abcdefgh | cut -c1-3,6-8
    • Outputs abcfgh

Examples of use of similar tools in cut's domain:

  • echo a  b|awk '{print $2}'
    • Displays "b", separating fields by any number of tabs or spaces. Thus, for awk, "b" is the 2nd field, while for cut -d" ", "b" is the 3rd field.
  • echo a  b|awk '{print $2 $1}'
    • Displays "ba", preserving the order of the fields as specified.
  • echo a  b|sed "s/  */ /g" |cut -f2 -d" "
    • Displays "b". Uses sed to first compact a sequence of spaces into a single space.

Limitations edit

Cut is subject to the following limitations:

  • There is no way to specify "one or more spaces" or the like as the field delimiter.
  • There is no way to change the order of the fields on the output.

Note: Some people find that awk or gawk is able to meet needs where cut is too limited.

Versions edit

A version of GNU cut for MS Windows is available from GnuWin32 project, as well as from Cygwin.

External links edit