Linux cut command

Linux 命令大全Linux Command Encyclopedia

The Linux cut command is used to display the text from num1 to num2 counting from the beginning of each line.

Syntax

cut  [-bn] [file]
cut [-c] [file]
cut [-df] [file]

Instructions:

The cut command cuts bytes, characters, and fields from each line of a file and writes these bytes, characters, and fields to standard output.

If the File parameter is not specified, the cut command reads standard input. One of the -b, -c, or -f flags must be specified.

Parameters:

  • -b: Split in bytes. These byte positions will ignore multi-byte character boundaries unless the -n flag is also specified.
  • -c: Split in characters.
  • -d: Custom delimiter, default is a tab.
  • -f: Used with -d to specify which region to display.
  • -n: Cancel splitting multi-byte characters. Only used with the -b flag. If the last byte of a character falls within the range indicated by the List parameter of the -b flag,
    the character will be written out; otherwise, the character will be excluded.

Examples

When you execute the who command, it outputs something like the following:

$ who
rocrocket :0           2009-01-08 11:07
rocrocket pts/0        2009-01-08 11:23 (:0.0)
rocrocket pts/1        2009-01-08 14:15 (:0.0)

If we want to extract the 3rd byte of each line, do this:

$ who|cut -b 3
c
c

Linux 命令大全Linux Command Encyclopedia

Other extensions