Linux 管理员 - cut 命令

cutgrep 是 CentOS 管理员最有用和最常用的两个命令。 cut 对于处理分隔文件(例如 Linux 配置文件、Linux 首选项文件和 CSV 文件)非常有用。

开关 操作
-b 仅选择这些字节
-c 仅选择这些字符
-d 使用 DELIM 而不是 TAB 作为字段分隔符
-s 仅打印分隔行

大多数情况下,cut 将用于从文本文件中提取特定行。之前,我们使用 cut 从 /etc/passwd 获取所有用户列表 −

[root@centosLocal centos]# cut -d":" -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown

以上是来自 /etc/passwd 的系统用户摘要列表。

一些 Linux 实用程序和应用程序实际上在保存输出时考虑到了 cut 的功能。以下是 nmap 输出的示例。

[root@centosLocal centos]# grep open ./http_scans.txt
Host: 10.58.52.67 () Ports: 80/open/tcp//http///
Host: 10.58.52.132 () Ports: 80/open/tcp//http///
Host: 10.58.52.133 () Ports: 80/open/tcp//http///
Host: 10.58.52.56 () Ports: 80/open/tcp//http///
Host: 10.58.52.71 () Ports: 80/open/tcp//http///
Host: 10.58.52.132 () Ports: 80/open/tcp//http///

使用 cut,我们可以快速生成一个使用端口 80 监听外部请求的内部系统列表。

[root@centosLocal centos]# grep open ./http_scans.txt | cut -d" " -f2 >
open_http_servers.txt  
[root@centosLocal centos]# head open_http_servers.txt  
10.58.52.17 
10.58.52.29 
10.58.52.30 
10.58.52.36 
10.58.52.59 
10.58.53.89 
10.58.53.100 
10.58.54.103 
10.58.54.148 
10.58.54.152

[root@centosLocal centos]#

cut 也可以根据字符数进行。

[root@centosLocal centos]# cut -c 1,2,3,4,5,6,7,8 lanIP-range.txt  
10.58.52 
10.58.52 
10.58.52 
10.58.52 
10.58.52 
10.58.52
10.58.53 
10.58.53 
10.58.53 
10.58.53 
10.58.53 
10.58.54 
10.58.54 
10.58.54 
10.58.54 
10.58.54 
10.58.54 
10.58.54 
10.58.54 
10.58.54 
10.58.54 
10.58.54 
10.58.54 
10.58.54 
10.58.54 
10.58.54 
10.58.54

[root@centosLocal centos]#

cut 是 CentOS 管理员几乎每天都会用到的命令。它是解析文本和一些二进制文件的救星。

basic_centos_linux_commands.html