Use of cut command

Time:2021-7-25

cutIt is used to intercept a part of each line of file or standard input.

usecutYou must specify one of the following three options:

-b byte-listIntercept by byte
-c character-listIntercept according to characters
-f field-listIntercept according to paragraph

The above three parametersbyte-list character-list field-listIs one or more numbers and ranges separated by commas-Range of connections). The actual positions of bytes, characters, and segments are1Alternatively, the scope of completion may not be given:-mEquivalent to1-m; n-Equivalent tonTo the end of the line.

For example, to intercept a standard input line2-5Characters between

$ cut -b2-5
ABCDEFG # here is the standard input
bcde

intercept1-3Between characters and5Characters

$ cut -c1-3,5
abcdefg
abce

Of course, you can also intercept every line in the file

$ cat cut.txt
James    23
Kobe    24
Wade    3

$ cut -c1-5 cut.txt
James
Kobe
Wade

PressparagraphIntercept, hereparagraphIt refers to each linetabTab (default) delimited segments. For example, we’re going to intercept the second paragraph

$ cat cut.txt
James    23
Kobe    24
Wade    3

$ cut -f2 cut.txt
23
24
3

You can also specify a separator, using-doption

$ cat cut.txt
James,23
Kobe,24
Wade,3

$cut - D, - F1 cut.txt # specifies the separator,
James
Kobe
Wade

When there is no specified delimiter in the row, you can use-sOption to ignore processing this row

$ cat cut.txt
James,23
Kobe,24
Wade,3
NBA ALL STAR

#Do not use - S
$ cut -d, -f1 cut.txt
James
Kobe
Wade
NBA ALL STAR

#Use - S
$ cut -d, -f1 -s cut.txt
James
Kobe
Wade

cutIt is often convenient to use it in combination with pipelines in scripts, such as how many lines of a file to get

$ wc -l config.json
7 config.json
#If you only want numbers, you can write them like this
$ wc -l config.json | cut -d' ' -f1
7

reference resources:GNU coreutils cut-invocation