国外哪些网站可以兼职做任务,wordpress怎么适应手机,太原建设银行保安招聘网站,怎么做网站聊天视频直播0 前言
在
Linux shell编程学习笔记42#xff1a;md5sum
中#xff0c;md5sum命令计算md5校验值后返回信息的格式是#xff1a; md5校验值 文件名 包括两项内容#xff0c;前一项是md5校验值 #xff0c;后一项是文件名。
如果我们只想要前面的md5 校验值#xff0c…
0 前言
在
Linux shell编程学习笔记42md5sum
中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, --bytesLIST select only these bytes -c, --charactersLIST select only these characters -d, --delimiterDELIM use DELIM instead of TAB for field delimiter -f, --fieldsLIST select only these fields; also print any line that contains no delimiter character, unless the -s option is specified -n with -b: dont split multibyte characters --complement complement the set of selected bytes, characters or fields -s, --only-delimited do not print lines not containing delimiters --output-delimiterSTRING 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 Nth byte, character or field, counted from 1 N- from Nth byte, character or field, to end of line N-M from Nth to Mth (included) byte, character or field -M from first to Mth (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, --bytesLIST仅选择这些字节-c, --charactersLIST仅选择这些字符-d, --delimiterDELIM使用 DELIM 而不是 TAB 作为字段分隔符必须与-b、-c或-f选项一起使用-f, --fieldsLIST 只显示选择的字段 除非指定了 -s 选项否则打印的内容不包含分隔符 -n -b 取消分割多字节字符。仅和 -b 标志一起使用。 如果字符的最后一个字节落在由 -b 标志的 List 参数指示的范围之内该字符将被输出否则该字符将被排除 --complement 显示选定的字节、字符集或字段的补集 即显示非选定的字节、字符集或字段 必须与-b、-c或-f选项一起使用-s, --only-delimited 不打印不包含分隔符的行 即只打印包含分隔符的行 --output-delimiterSTRING 使用 STRING 作为输出分隔符 默认设置是使用输入分隔符 --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-delimiterSTRING 使用指定字符串作为输出分隔符 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作为分隔符的且记在这里备忘。