📜  Linux 中的 cut 命令示例

📅  最后修改于: 2022-05-13 01:57:34.322000             🧑  作者: Mango

Linux 中的 cut 命令示例

UNIX 中的 cut 命令是用于从文件的每一行中切出节并将结果写入标准输出的命令。它可用于按字节位置、字符和字段切割部分行。基本上, cut 命令将一行切片并提取文本。必须在命令中指定选项,否则会出错。如果提供了多个文件名,则来自每个文件的数据不会在其文件名之前

句法:

cut OPTION... [FILE]...

让我们考虑一个具有名称state.txt两个文件,分别capital.txt包含印度的国家和首都的名称5。

$ cat state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh

如果没有指定任何选项,它会显示错误。

$ cut state.txt
cut: you must specify a list of bytes, characters, or fields
Try 'cut --help' for more information.

选项及其说明和示例:



1. -b(byte):要提取特定的字节,您需要在-b 选项后面加上以逗号分隔的字节数列表。也可以使用连字符 (-) 指定字节范围。必须指定字节数列表,否则会出错。制表符和退格符被视为 1 个字节的字符。

List without ranges
$ cut -b 1,2,3 state.txt
And
Aru
Ass
Bih
Chh

List with ranges
$ cut -b 1-3,5-7 state.txt
Andra
Aruach
Assm
Bihr
Chhtti

它使用一种特殊的形式来选择从行首到行尾的字节:

In this, 1- indicate from 1st byte to end byte of a line
$ cut -b 1- state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh

In this, -3 indicate from 1st byte to 3rd byte of a line
$ cut -b -3 state.txt
And
Aru
Ass
Bih
Chh

2. -c(列):要按字符剪切,请使用 -c 选项。这将选择提供给 -c 选项的字符。这可以是由逗号分隔的数字列表或由连字符 (-) 分隔的数字范围。制表符和退格符被视为字符。必须指定字符编号列表,否则此选项会出错。

句法:

$cut -c [(k)-(n)/(k),(n)/(n)] filename

这里, k表示字符的起始位置, n表示字符在每一行的结束位置,如果kn用“-”分隔,否则它们只是文件中每行字符的位置作为一个输入。

$ cut -c 2,5,7 state.txt
nr
rah
sm
ir
hti

以上 cut 命令从文件的每一行打印第二个、第五个和第七个字符。

$ cut -c 1-7 state.txt
Andhra
Arunach
Assam
Bihar
Chhatti

以上 cut 命令打印文件中每行的前七个字符。

Cut 使用一种特殊的形式来选择从行首到行尾的字符:



$ cut -c 1- state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh

Above command prints starting from first character to end. Here in command only starting
position is specified and the ending position is omitted.

$ cut -c -5 state.txt
Andhr
Aruna
Assam
Bihar
Chhat

Above command prints starting position to the fifth character. Here the starting position
is omitted and the ending position is specified.

3. -f (field): -c选项对于固定长度的行很有用。大多数 unix 文件没有固定长度的行。要提取有用的信息,您需要按字段而不是列进行剪切。指定的字段编号列表必须用逗号分隔。 -f 选项不描述范围cut使用制表符作为默认字段分隔符,但也可以使用-d选项与其他分隔符一起使用。
注意:在 UNIX 中,空格不被视为分隔符。

句法:

$cut -d "delimiter" -f (field number) file.txt

就像在文件state.txt 中一样,如果未使用 -d 选项,则字段由空格分隔,然后它会打印整行:

$ cut -f 1 state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh

如果使用 -d 选项,则它将空格视为字段分隔符或定界符:

$ cut -d " " -f 1 state.txt
Andhra
Arunachal
Assam
Bihar
Chhattisgarh
Command prints field from first to fourth of each line from the file.
Command:
$ cut -d " " -f 1-4 state.txt
Output:
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh

4. –complement:顾名思义,它补充输出。此选项可以与其他选项结合使用-f-c

$ cut --complement -d " " -f 1 state.txt
Pradesh
Pradesh
Assam
Bihar
Chhattisgarh

$ cut --complement -c 5 state.txt
Andha Pradesh
Arunchal Pradesh
Assa
Biha
Chhatisgarh

5. –output-delimiter:默认情况下,输出分隔符与我们在带有-d选项的剪切中指定的输入分隔符相同。要更改输出分隔符,请使用选项–output-delimiter="delimiter"

$ cut -d " " -f 1,2 state.txt --output-delimiter='%'
Andhra%Pradesh
Arunachal%Pradesh
Assam
Bihar
Chhattisgarh

这里 cut 命令更改标准输出中使用 -f 选项指定的字段之间的分隔符(%)。

6. –version:此选项用于显示当前在您系统上运行的 cut 版本。

$ cut --version
cut (GNU coreutils) 8.26
Packaged by Cygwin (8.26-2)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David M. Ihnat, David MacKenzie, and Jim Meyering.

cut 命令的应用

1.如何使用tail with pipe(|): cut命令可以与unix的许多其他命令一起使用。在下面的示例中, cat命令的输出作为带有-f选项的cut命令的输入,以相反的顺序对来自文件 state.txt 的状态名称进行排序。

$ cat state.txt | cut -d ' ' -f 1 | sort -r
Chhattisgarh
Bihar
Assam
Arunachal
Andhra

它还可以通过管道连接一个或多个过滤器以进行额外处理。就像在下面的例子中一样,我们使用 cat、head 和 cut 命令,其输出使用指令(>)存储在文件名 list.txt 中。

$ cat state.txt | head -n 3 | cut -d ' ' -f 1 > list.txt

$ cat list.txt
Andhra
Arunachal
Assam

感谢Saloni Gupta提供更多示例。