该命令有两项功能,其一是用来显示文件的内容,它依次读取由参数file所指明的文件,将它们的内容输出到标准输出上;其二是连接两个或多个文件,如cut fl f2 > f3将把文件fl和f2的内容合并起来,然后通过输出重定向符“>”的作用,将它们放入文件f3中。
当文件较大时,文本在屏幕上迅速闪过(滚屏),用户往往看不清所显示的内容。因此,一般用more等命令分屏显示。
为了控制滚屏,可以按Ctrl+S键,停止滚屏;按Ctrl+Q键可以恢复滚屏。按Ctrl+C(中断键可以终止该命令的执行,并且返回Shell提示符状态。
# cut (选项) (参数)
文件:指定要进行内容过滤的文件
cut的缺点是在处理多空格时。如果文件里面的某些域是由若干个空格来间隔的,那么用cut就有点麻烦了,因为cut只擅长处理“以一个字符间隔”的文本内容
[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
按域cut
以/etc/passwd文件为例:
[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来取域就可以了
原文:https://www.cnblogs.com/sunjianlin/p/13248550.html