Linux中的cut命令及其示例

UNIX中的cut命令用于从每行文件中剪切部分,并将结果写入标准输出。它可以用来切割一条线的一部分 字节位置、字符和字段 .基本上,“剪切”命令会分割一行并提取文本。必须用命令指定选项,否则会出现错误。如果提供了多个文件名,则每个文件中的数据都会被删除 不先于 通过它的文件名。

null

语法:

cut OPTION... [FILE]...

让我们考虑两个文件的名称。 状态txt 首都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(字节): 要提取特定的字节,需要遵循-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 表示每行中字符的结束位置,如果 K N 用“-”分隔,否则它们只是作为输入的文件中每行字符的位置。

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

上面的“剪切”命令从文件的每一行打印第二、第五和第七个字符。

$ 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(现场): -c 该选项适用于固定长度的线。大多数unix文件没有固定长度的行。要提取有用的信息,需要按字段而不是列进行剪切。指定字段编号的列表必须用逗号分隔。 范围不使用-f选项描述 . 使用 标签 作为默认字段分隔符,但也可以通过使用 -d 选项 注: 在UNIX中,空格不被视为分隔符。

语法:

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

比如档案里 状态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.补充: 顾名思义,它是对输出的补充。此选项可与其他选项结合使用,包括 -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.–输出分隔符: 默认情况下,输出分隔符与我们在剪切中指定的输入分隔符相同 -d 选项要更改输出分隔符,请使用选项 –output delimiter=“delimiter” .

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

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

6.–版本: 此选项用于显示系统上当前运行的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.如何使用尾管(|): cut命令可以与unix的许多其他命令进行管道连接。在下面的示例中 命令作为输入提供给 指挥 -f 选项对来自文件状态的状态名称进行排序。以相反的顺序。

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

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

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

$ cat list.txt
Andhra
Arunachal
Assam

谢谢 萨洛尼·古普塔 为了提供更多的例子。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享