Linux shell编程学习笔记43:cut命令

0 前言

Linux shell编程学习笔记42:md5sum

中,md5sum命令计算md5校验值后返回信息的格式是:

md5校验值 文件名

包括两项内容,前一项是md5校验值 ,后一项是文件名。

如果我们只想要前面的md5 校验值,可以使用cut命令来实现。

1 cut命令的功能和格式

我们可以使用命令 cut --help命令 查看它的用法:

purpleEndurer @ bash ~ $ cut --help

Usage: cut OPTION... [FILE]...

Print selected parts of lines from each FILE to standard output.

Mandatory arguments to long options are mandatory for short options too.

-b, --bytes=LIST select only these bytes

-c, --characters=LIST select only these characters

-d, --delimiter=DELIM use DELIM instead of TAB for field delimiter

-f, --fields=LIST select only these fields; also print any line

that contains no delimiter character, unless

the -s option is specified

-n with -b: don't split multibyte characters

--complement complement the set of selected bytes, characters

or fields

-s, --only-delimited do not print lines not containing delimiters

--output-delimiter=STRING use STRING as the output delimiter

the default is to use the input delimiter

--help display this help and exit

--version output version information and exit

Use one, and only one of -b, -c or -f. Each LIST is made up of one

range, or many ranges separated by commas. Selected input is written

in the same order that it is read, and is written exactly once.

Each range is one of:

N N'th byte, character or field, counted from 1

N- from N'th byte, character or field, to end of line

N-M from N'th to M'th (included) byte, character or field

-M from first to M'th (included) byte, character or field

With no FILE, or when FILE is -, read standard input.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/\>

Report cut translation bugs to <http://translationproject.org/team/\>

For complete documentation, run: info coreutils 'cut invocation'

purpleEndurer @ bash ~ $

1.1 cut命令的功能

cut命令的功能是将文本行中的选定部分打印到标准输出。

1.2 cut命令的格式

cut 选项... [文件] ...

1.2.1 选项及功能

选项 描述 备注
-b, --bytes=LIST 仅选择这些字节
-c, --characters=LIST 仅选择这些字符
-d, --delimiter=DELIM 使用 DELIM 而不是 TAB 作为字段分隔符 必须与-b、-c或-f选项一起使用
-f, --fields=LIST 只显示选择的字段 除非指定了 -s 选项,否则打印的内容不包含分隔符
-n -b 取消分割多字节字符。仅和 -b 标志一起使用。 如果字符的最后一个字节落在由 -b 标志的 List 参数指示的范围之内,该字符将被输出;否则,该字符将被排除
--complement 显示选定的字节、字符集或字段的补集 即:显示非选定的字节、字符集或字段 必须与-b、-c或-f选项一起使用
-s, --only-delimited 不打印不包含分隔符的行 即:只打印包含分隔符的行 必须与-b、-c或-f选项一起使用
--output-delimiter=STRING 使用 STRING 作为输出分隔符 默认设置是使用输入分隔符 必须与-b、-c或-f选项一起使用
--help 显示此帮助并退出
--version 输出版本信息并退出

1.2.2 选择的表示方法

1.2.2.1 选择方法1

指定字节、字符或字段号,号之间以半角逗号分隔,例如:

1,3,5:选择第1、第3、第5个字节、字符或字段号

1.2.2.2 选择方法2

指定字节、字符、字段的起始号和结束号,两个之间用半角剪号分隔,列如:

超始号N-结束号M:从第N个字节、字符、字段到第M个(包括第M个)

如果不指定开始号,则默认从行首(第1个)开始,如:

-结束号N:从行首到第N个字节、字符、字段结束(包括第N个)

如果不指定结束号,则默认到行末,如:

起始号N-:从第N个字节、字符、字段开始(包括第N个),直到行末

以上两种表示方法可以混用 ,其间以半角逗号分隔。

2 cut命令使用实例

2.0 创建演示文件

我们先创建一个演示用的文件a.txt,内容如下:

no name music sport

1 aa 100 100

1 bb 99 99

创建文件和查看文件内容的命令序列如下:

purpleEnduer @ bash ~ $ ++echo "no name music sport" > a.txt++

purpleEnduer @ bash ~ $ ++echo "1 aa 100 100" >> a.txt++

purpleEnduer @ bash ~ $ ++echo "1 bb 99 99" >> a.txt++

purpleEnduer @ bash ~ $ ++cat a.txt++

no name music sport

1 aa 100 100

1 bb 99 99

2.1 cut -b:按字节选择

2.1.1 cut -b 1 a.txt:显示文件各行的第1个字节

purpleEnduer @ bash ~ $ ++cut -b 1 a.txt++

n

1

1

2.1.2 cut -b 1,2,5 a.txt:显示文件各行的第1、第2和第5个字节

purpleEnduer @ bash ~ $ ++cut -b 1,2,5 a.txt++

noa

1

1

2.1.3 cut -b 1-5 a.txt:显示文件各行的第1-5个字节内容

purpleEnduer @ bash ~ $ ++cut -b 1-5 a.txt++

no na

1 aa

1 bb

2.1.4 cut -b 1-5 a.txt:显示文件各行的第1-5个字节和第8-10个字节的内容

purpleEnduer @ bash ~ $ ++cut -b 1-5,8-10 a.txt++

no na mu

1 aa 0 1

1 bb 9

2.1.5 cut -b 1-5 a.txt:显示文件各行的第1个字节,第5个字节和第1-5个字节及第8-10个字节的内容

purpleEnduer @ bash ~ $ ++cut -b 1,5,1-5,8-10 a.txt++

no na mu

1 aa 0 1

1 bb 9

2.2 cut -c: 按字符选择

2.2.1 cut -c 1 a.txt:显示文件各行的第1个字符

purpleEnduer @ bash ~ $ ++cut -c 1 a.txt++

n

1

1

2.2.2 cut -c 1,8 a.txt:显示文件各行的第1个和第8个字符

purpleEnduer @ bash ~ $ ++cut -c 1,8 a.txt++

n

10

19

2.2.3 cut -c 1-5 a.txt:显示文件各行的第1个-第5个字符

purpleEnduer @ bash ~ $ ++cut -c 1-5 a.txt++

no na

1 aa

1 bb

2.2.4 cut -c 1-5,7,9 a.txt:显示文件各行的第1个-第5个字符,第7个字符和第9个字符

purpleEnduer @ bash ~ $ ++cut -c 1-5,7,9 a.txt++

no naem

1 aa 0

1 bb 9

2.2 cut -f -d:按字段选择

2.2.0 cut -f 1 a.txt:显示文件各行的第1个字段

purpleEnduer @ bash ~ $ cut -f 1 a.txt

no name music sport

1 aa 100 100

1 bb 99 99

结果文件内容全部显示出来了,这是因为系统默认字段是以\t来分隔的,而我们创建a.txt时是以空格来分隔的。

2.2.1 cut -f 1 -d ' ' a.txt:显示文件各行的第1个字段

这时我们如果仍然要选择字段,我们就要同时使用-d选项。

purpleEnduer @ bash \w $ cut -f1 -d ' ' a.txt

no

1

1

2.2.2 cut -f 3- -d ' ' a.txt:从第3个字段开始显示文件各行

purpleEnduer @ bash \w $ cut -f 3- -d ' ' a.txt

music sport

100 100

99 99

下面我们重新创建文件a.txt,并以\t来分隔各字段,命令序列如下:

purpleEnduer @ bash ~ $ ++echo -e "no\tname\tmusic\tsport" > a.txt++

purpleEnduer @ bash ~ $ ++echo -e "1\taa\t100\t100" >> a.txt++

purpleEnduer @ bash ~ $ ++echo -e "1\tbb\t99\t99" >> a.txt++

purpleEnduer @ bash ~ $ ++cat a.txt++

no name music sport

1 aa 100 100

1 bb 99 99

为了让echo命令将\t识别为转义字符,我们使用了-e选项。

请注意-e选项的位置,它是直接跟在echo 命令后面的。

这样我们可以使用命令: cut -f 2-4 a.txt 来选择第2个-4个字段显示而不必用-d选项了:

purpleEnduer @ bash ~ $ ++cut -f 2-4 a.txt++

name music sport

aa 100 100

bb 99 99

2.3 cut --complement:显示非选定的内容

为了展示--complement的功能,我们可以对比以下几组命令的返回信息,来理解--complement的功能:

对于以空格做为字段分隔符的文件:

purpleEnduer @ bash \w $ ++cut -f 3 -d ' ' a.txt++

music

100

purpleEnduer @ bash \w $ ++cut -f 3 -d ' ' --complement a.txt++

no name sport

1 aa 100

1 bb 99 99

purpleEnduer @ bash \w $

对于以\t做为字段分隔符的文件:

purpleEnduer @ bash \w $ ++cut -f 3 a.txt++

music

100

99

purpleEnduer @ bash \w $ ++cut -f 3 --complement a.txt++

no name sport

1 aa 100

1 bb 99

purpleEnduer @ bash \w $

再来一组:

purpleEnduer @ bash ~ $ cut -f 2-3 a.txt

name music

aa 100

bb 99

purpleEnduer @ bash ~ $ cut -f 2-3 --complement a.txt

no sport

1 100

1 99

purpleEnduer @ bash ~ $

2.4 cut -s:只打印包含分隔符的行

我们通过分析下列命令的执行情况来理解这个选项的功能。

purpleEnduer @ bash ~ $ ++cut -s a.txt++

cut: you must specify a list of bytes, characters, or fields

Try 'cut --help' for more information.

-s 选项必须和-b、-c或-f选项联合使用。

purpleEnduer @ bash ~ $++cut -f 3 -s a.txt++

music

100

99

由于a.txt中的字段是以\t分隔的,所以可以正常显示第3个字段的内容。

purpleEnduer @ bash ~ $ cut -f 3 -s -d ' ' a.txt

由于a.txt中的字段是以\t分隔的,所以当我们指定分隔符为空格时,没有一行符合这个要求,所以命令执行结果为空,因为没有可以显示的内容。

purpleEnduer @ bash ~ $ cut -f 3 -s -d a a.txt

100 100

purpleEnduer @ bash ~ $

我们指定字符a为分隔符,这个执行结果有点难理解。

我们先看文件a.txt的内容:

no name music sport

1 aa 100 100

1 bb 99 99

第1行内容只包含一个字符a,以字符a作为分隔符的话,这行只有2个字段:

第1个字段:no\tn

第2个字段: me\tmusic\tsport

而命令选项-f 3 要求显示第3列,所以第1行尽管包含有分隔符a,但没有第3个字段,所以这行显示为空。

第2行内容包含了两个字符a,以字符a作为分隔符的话,这行包括3个字符:

第1 个字段:1\t

第2个字段:空(位于aa之间)

第3个字段:\t100\t100

所以第2行显示了第3个字段。

第3行内容不包括分融符a,所以这一行不显示。

2.5 cut --output-delimiter=STRING :使用指定字符串作为输出分隔符

purpleEnduer @ bash ~ $++echo -e "no\tname\tmusic\tsport" > a.txt++

purpleEnduer @ bash ~ $ ++echo -e "1\taa\t100\t100" >> a.txt++

purpleEnduer @ bash ~ $ ++echo -e "1\tbb\t99\t99" >> a.txt++

purpleEnduer @ bash ~ $ ++cat a.txt++

no name music sport

1 aa 100 100

1 bb 99 99

purpleEnduer @ bash ~ $ ++cut --output-delimiter=* a.txt++

cut: you must specify a list of bytes, characters, or fields

Try 'cut --help' for more information.

purpleEnduer @ bash ~ $ ++cut -f --output-delimiter=* a.txt++

cut: invalid byte, character or field list

Try 'cut --help' for more information.

purpleEnduer @ bash ~ $ ++cut -f 1- --output-delimiter=* a.txt++

no*name*music*sport

1*aa*100*100

1*bb*99*99

purpleEnduer @ bash ~ $

这个选项必须和-b、-c或-f配合使用,所以命令

cut --output-delimiter=* a.txt

cut -f --output-delimiter=* a.txt

都出错了。

命令 cut -f 1- --output-delimiter=* a.txt 指定分隔符为*,所以显示的结果就是:

no*name*music*sport

1*aa*100*100

1*bb*99*99

原先的\t被替换为*了。

3.后记

在学习本节内容时,为了方便,示例文件a.txt的内容是以空格作为分隔符的,由于linux默认分隔符是\t,为了说明命令的功能,又增加了是以\t作为分隔符的示例文件,仍然以a.txt为文件名,如果不仔细看,容易引起混乱,以后有机会对这个笔记进行修订时,就改为使用两个示例文件,其中示例文件a.txt的内容是以空格作为分隔符的,示例文件b.txt的内容是以\t作为分隔符的,且记在这里备忘。

相关推荐
月清晖34 分钟前
centos更换yum源、安装Docker和换源
linux·docker·centos
【 教主 】1 小时前
<Linux> 多线程
linux·运维·服务器
技术探索者1 小时前
Shell:如何判断两个字符串相等
linux·shell
Slow1 小时前
Linux静态库的制作
linux·c语言
一个梦想过上五休二生活的男人1 小时前
Firewalld防火墙(二)
linux·服务器·数据库
一只猿Hou1 小时前
【logback-spring配置不生效,开发环境和生产环境配置不同输出级别】
linux·spring·logback
gopher95112 小时前
Linux多进程和多线程(五)进程间通信-消息队列
linux·服务器·c语言·开发语言·进程
efls1112 小时前
Linux_管道通信
linux·运维·服务器·管道
frans4x2 小时前
操作系统僵尸进程、CFS、上下文切换
linux·服务器
口嗨农民工3 小时前
uboo对内存操作读写命令的基本使用
linux·ubuntu·c·uboot