cut命令
该命令有两项功能
其一是用来显示文件的内容,它依次读取由参数file所指明的文件,将它们的内容输出到标准输出上;
其二是连接两个或多个文件,如cut fl f2 > f3将把文件fl和f2的内容合并起来,然后通过输出重定向符“>”的作用,将它们放入文件f3中。
cut (选项) (参数)
| 选项 | 参数 |
|---|---|
| -b: | 仅显示行中指定直接范围的内容; |
| -c: | 仅显示行中指定范围的字符; |
| -d: | 指定字段的分隔符,默认的字段分隔符为“TAB”; |
| -f: | 与-d一起使用,显示指定字段的内容; |
| -n: | 与“-b”选项连用,不分割多字节字符; |
| --complement: | 补足被选择的字节、字符或字段; |
| --out-delimiter=<字段分隔符>: | 指定输出内容是的字段分割符; |
| --help: | 显示指令的帮助信息; |
| --version: | 显示指令的版本信息。 |
常用的三种方式
字节(bytes),用选项-b,一个空格算一个字节,一个汉字算三个字节
字符(characters),用选项-c
域(fields),用选项-f
示例
[root@VM_0_9_centos ~]# who
root pts/0 2020-07-05 18:49 (59.109.219.127)
[root@VM_0_9_centos ~]# who |cut -b 3
o
[root@VM_0_9_centos ~]# who |cut -b 4
t
[root@VM_0_9_centos ~]# who |cut -b 2-10
oot p
[root@VM_0_9_centos ~]# who |cut -b 18-30
2020-07-
[root@VM_0_9_centos ~]# who |cut -b 25-30
20-07-
[root@VM_0_9_centos ~]# who |cut -b 23-30
2020-07-
[root@VM_0_9_centos ~]# who |cut -b 23-33
2020-07-05
示例二
[root@VM_0_9_centos ~]# cat /etc/passwd|cut -d : -f 1
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
uucp
operator
games
gopher
ftp
nobody
vcsa
abrt
ntp
saslauth
postfix
sshd
dbus
tcpdump
syslog
mysql
apache
7
nginx
[root@VM_0_9_centos ~]# cat /etc/passwd|head -n5|cut -d : -f 1
root
bin
daemon
adm
lp
[root@VM_0_9_centos ~]# cat /etc/passwd|head -n5|cut -d : -f 1,3-5,7
root:0:0:root:/bin/bash
bin:1:1:bin:/sbin/nologin
daemon:2:2:daemon:/sbin/nologin
adm:3:4:adm:/sbin/nologin
lp:4:7:lp:/sbin/nologin
示例三 (按字符cut相对比较简单,中文字符和空格都算一个字符)
Sun Jul 5 18:58:41 CST 2020
[root@VM_0_9_centos ~]# date |cut -c 1-5
Sun J
[root@VM_0_9_centos ~]# date |cut -c 1-8
Sun Jul
[root@VM_0_9_centos ~]# date |cut -c 15
5
[root@VM_0_9_centos ~]# date |cut -c 25
2
[root@VM_0_9_centos ~]# date |cut -c 25-28
2020
按域
[root@VM_0_9_centos ~]# head -n5 /etc/passwd |cut -d : -f 1,3-5
root:0:0:root
bin:1:1:bin
daemon:2:2:daemon
adm:3:4:adm
lp:4:7:lp
-d指定域分隔符,-f 指定要剪出哪几个域,这个与awk的输出特定字段功能一样。
-d选项的默认间隔符就是制表符,所以当你就是要使用制表符的时候,完全就可以省略-d选项,而直接用-f来取域就可以了
浙公网安备 33010602011771号