cut命令、sort命令、uniq命令使用

11次阅读

共计 2968 个字符,预计需要花费 8 分钟才能阅读完成。

cut 命令
含义正如 cut 的中文意思,cut 的工作就是“剪”,具体来说是在文件负责剪切数据用的,它以每一行为一个处理对象,与 sed 的机制是一样的。
剪切依据 cut 命令主要是接受三个定位方法:

第一,字节(bytes),用选项 -b

第二,字符(characters),用选项 -c

第三,域(fields),用选项 -f

语法格式
cut [-bn] [file] 或 cut [-c] [file] 或 cut [-df] [file]

使用说明 cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段写至标准输出。如果不指定 File 参数,cut 命令将读取标准输入。必须指定 -b、-c 或 -f 标志之一。主要参数

-b:以字节为单位进行分割。这些字节位置将忽略多字节字符边界,除非也指定了 -n 标志。

-c:以字符为单位进行分割。

-d:自定义分隔符,默认为制表符。

-f:与 - d 一起使用,指定显示哪个区域。

-n:取消分割多字节字符。仅和 -b 标志一起使用。如果字符的最后一个字节落在由 -b 标志的 List 参数指示的范围之内,该字符将被写出;否则,该字符将被排除。

常用示例:“字节”定位中,提取第 1 到第 5 和第 10 个字节,- b 支持形如 1 - 5 的写法,而且多个定位之间用逗号隔开注意,cut 命令如果使用了 - b 选项,那么执行此命令时,cut 会先把 - b 后面所有的定位进行从小到大排序,然后再提取。
示例 1:提取第 1 到第 5 字节和第 10 个字节的内容
[root@moli_linux1 ~]$ echo “abcde12345” | cut -b 1-5,10
abcde5
示例 2:- 3 表示从第 1 个字节到第 3 个字节,3- 表示从第 3 个字节到行尾
[root@moli_linux1 ~]$ echo “abcde54321” | cut -b -3
abc
[root@moli_linux1 ~]$ echo “abcde54321” | cut -b 3-
cde54321
示例 3:多字节字符处理 1 个空格算 1 个字节,而 1 个汉字算 3 个字节,因此汉字是多字节字符. 遇到多字节字符可以使用 - c 选项,也可以使用 - b 选项,不过要结合 - n 选项,- n 选项告诉 cut 命令不要将多字节字符拆开。
[root@moli_linux1 ~]$ cat cut_test2.txt
星期一 aa
星期二 bb
星期三 cc
[root@moli_linux1 ~]$ cut -b 3 cut_test2.txt # 因为没加 - n 选项,因此将 ” 星 ” 字给拆分了..

[root@moli_linux1 ~]$ cut -nb 3 cut_test2.txt # 切割第 3 个字符



[root@moli_linux1 ~]$ cut -c 3 cut_test2.txt



[root@moli_linux1 ~]$ cut -c 4,5 cut_test2.txt
aa
bb
cc
[root@moli_linux1 ~]$ cut -nb 4,5 cut_test2.txt
aa
bb
cc
示例 4:对于非固定格式的文档,使用 - f 选项来定义域来切割文件
[root@moli_linux1 ~]$ head -n 5 /etc/passwd > cut_test.txt
[root@moli_linux1 ~]$ cat cut_test.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@moli_linux1 ~]$ cut -d “:” -f 1-3,5 cut_test.txt
root:x:0:root
bin:x:1:bin
daemon:x:2:daemon
adm:x:3:adm
lp:x:4:lp
上面的 cut -d “:” -f 1-3,5 cut_test.txt 中,- d 选项指定: 为分割符,每个分隔符作为一个域,- f 后面的数字指定切割哪个域,这里 1 -3,5 表示截取第 1,2,3 和第 5 个域。
sort 命令
说明 sort 命令将文件进行排序,并将排序结果标准输出。sort 命令既可以从特定的文件,也可以从 stdin 中获取输入。
工作原理 sort 将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按 ASCII 码值进行比较,最后将他们按升序输出。
语法
sort(选项)(参数)

常用选项

-u:unique 唯一,排序并且排除重复项

-n: number 按照数字进行排序,默认数字会被当作字符串进行比较,字符或者其他特殊符号会被当做 0 排在数字前面

-r: 反向排序

-b: 忽略每行前面开始出的空格字符

示例 1:- u 选项
[root@moli_linux1 ~]$ cat sort_test.txt
hello
world
>>>
hello
12345
shell
???
have a space
[root@moli_linux1 ~]$ sort -u sort_test.txt # 去除重复 hello 并排序
>>>
???
12345
have a space
hello
shell
world
示例 2:- n 选项。按照数字进行排序,默认数字会被当作字符串进行比较,字符或者其他特殊符号会被当做 0 排在数字前面
[root@moli_linux1 ~]$ cat sort_test2.txt
987
124
123
234
???
hello
123
[root@moli_linux1 ~]$ sort -n sort_test2.txt
???
hello
123
123
124
234
987
示例 3:反向排序 -r
[root@moli_linux1 ~]$ cat sort_test2.txt
987
124
123
234
???
hello
123
[root@moli_linux1 ~]$ sort -rn sort_test2.txt
987
234
124
123
123
hello
???
uniq 命令
说明 uniq 命令用于报告或忽略文件中的重复行,一般与 sort 命令结合使用。对于 uniq 来说,去除重复的内容,内容之间必须相邻,不然无法去除。因此多先用 sort 排序,再用 uniq 去重。
语法
uniq(选项)(参数)

常用选项

-c: 在每列旁边显示该行重复出现的次数

-d: 仅显示重复出现的行列;

示例 1
[root@moli_linux1 ~]$ cat uniq.txt
hello
world
hello
python
1
1
2
3
[root@moli_linux1 ~]$ uniq uniq.txt # 只会去除相邻的重复内容数字 1,不相邻的重复内容 hello 不会被删除
hello
world
hello
python
1
2
3
示例 2,结合 sort 删除所有重复内容
[root@moli_linux1 ~]$ sort uniq.txt |uniq
1
2
3
hello
python
world
示例 3:统计各行在文件中出现的次数
[root@moli_linux1 ~]$ sort uniq.txt |uniq -c
2 1
1 2
1 3
2 hello
1 python
1 world
示例 4:找出文件中重复的行
[root@moli_linux1 ~]$ sort uniq.txt |uniq -d
1
hello

总结:这三个命令经常在 shell 脚本中使用,除了 grep,sed,awk 三个之外,也需要熟练掌握。

正文完
 0