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.htm
廣告
© . All rights reserved.