1. 程式人生 > 實用技巧 >Linux cut 命令詳解

Linux cut 命令詳解

cut 命令在Linux和Unix中的作用是從檔案中的每一行中截取出一些部分,並輸出到標準輸出中。我們可以使用 cut 命令從一行字串中於以位元組,字元,欄位(分隔符)等單位擷取一部分內容出來。

在本文中,我們通過一些例子來了解 cut 命令的使用,這些使用方法在我們的日常工作中也是非常常用的。

Cut 命令和語法

cut 命令的基本語法如下:

$ cut OPTION... [FILE]...

我們先來了解一下 cut 的一些選項,cut 命令必須要指定選項才能執行。

-f : 提取指定的欄位,cut 命令使用 Tab 作為預設的分隔符。

-d : Tab 是預設的分隔符,使用這一選項可以指定自己的分隔符。

-b : 提取指定的位元組,也可以指定一個範圍。

-c : 提取指定的字元,可以是以逗號分隔的數字的列表,也可以是以連字元分隔的數字的範圍。

–complement : 補充選中的部分,即反選。

–output-delimiter : 修改輸出時使用的分隔符。

--only-delimited : 不輸出不包含分隔符的列。

我們以如下的名為 context.txt 的文字檔案和 /etc/passwd 檔案來為例來進行說明。

$ cat content.txt 
Ubuntu Linux
Microsoft Windows
OsX El Capitan
Unix
FreeBSD

如何指定分隔符

最常用的選項是 -d-f 的組合,這會根據 -d 指定的分隔符和 -f 列出的欄位來提取內容。

例如在這個例子中只打印出 /etc/passwd 檔案每一行的第一個欄位,用的分隔符是 :

$ cut -d':' -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
alvin
liangxu
...

在下面這個例子中我們用空格作為分隔符列印 content.txt 檔案的第一個欄位

$ cut -d " " -f 1 content.txt 
Ubuntu
Microsoft
OsX
Unix
FreeBSD

在下面這個例子中我們提取了多個欄位。這裡,我們使用冒號(:)分隔符從檔案 /etc/passwd 中包含字串 /bin/bash 的行提取第一和第六個欄位。

$ grep "/bin/bash" /etc/passwd | cut -d':' -f1,6
root:/root
alvin:/home/alvin

要顯示欄位的某個範圍,可以指定開始和結束的欄位,中間用連字元(-)連線,如下所示:

$ grep "/bin/bash" /etc/passwd | cut -d':' -f1-4,6,7
root:x:0:0:/root:/bin/bash
alvin:x:1000:1000:/home/alvin:/bin/bash

如何補全選擇的輸出

要補全選擇輸出的欄位(即反選),使用 --complement 選項。這一選項輸出所有的欄位,除了指定的欄位。

在下面這個例子中輸出 /etc/passwd 檔案中包含 /bin/bash 的行中除了第二個欄位以外的所有欄位:

$ grep "/bin/bash" /etc/passwd | cut -d':' --complement -f2
root:0:0:root:/root:/bin/bash

如何指定輸出的分隔符

使用 --output-delimiter 可以指定輸出的分隔符。輸入的分隔符由 -d 來指定,而輸出分隔符和輸入分隔符預設是一樣的。

我們先以下面的例子來測試不指定輸出分隔符時的輸出;

$  cut -d: -f1,7  /etc/passwd |  sort |  uniq -u
_apt:/usr/sbin/nologin
backup:/usr/sbin/nologin
bin:/usr/sbin/nologin
daemon:/usr/sbin/nologin
dnsmasq:/usr/sbin/nologin
games:/usr/sbin/nologin
gnats:/usr/sbin/nologin
irc:/usr/sbin/nologin
landscape:/usr/sbin/nologin
list:/usr/sbin/nologin
lp:/usr/sbin/nologin
lxd:/bin/false

現在我們加上--output-delimiter選項,將輸出分隔符指定為空格:

$  cut -d: -f1,7 --output-delimiter ' ' /etc/passwd |  sort |  uniq -u
_apt /usr/sbin/nologin
backup /usr/sbin/nologin
bin /usr/sbin/nologin
daemon /usr/sbin/nologin
dnsmasq /usr/sbin/nologin
games /usr/sbin/nologin
gnats /usr/sbin/nologin
irc /usr/sbin/nologin
landscape /usr/sbin/nologin
list /usr/sbin/nologin
lp /usr/sbin/nologin
lxd /bin/false

我們再測試一個例子,用分隔符讓每一行列印一個欄位。

我們將 --output-delimiter 指定為 $'\n' 表換行。

輸出結果為:

$ grep root /etc/passwd | cut -d':' -f1,6,7 --output-delimiter=$'\n'
root
/root
/bin/bash
operator
/root
/sbin/nologin

如何以字元的方式提取內容

-c選項可以用來根據字元位置進行提取,注意空格和Tab也以字元來處理。

列印 context.txt 檔案每一行的第一個字元,如下:

$ cut -c 1 content.txt
U
M
O
U
F

下面顯示了 context.txt 檔案每一行的第一至七個字元;

$ cut -c 1-7 content.txt
Ubuntu
Microso
OsX El
Unix
FreeBSD

我們再測試一下只指定開始或結束的位置。

下面提取第二個到最後一個字元:

$ cut -c2- content.txt
buntu Linux
icrosoft Windows
sX El Capitan
nix
reeBSD

提取第一到第四個字元:

cut -c-4 content.txt
Ubun
Micr
OsX
Unix
Free

如何根據位元組提取

使用-b選項通過指定位元組的位置來選擇一行的某一部分,使用逗號分隔每個指定位置,或用連字元 - 指定一個範圍。

下面這個例子提取 content.txt 檔案每一行的第一,二,三個位元組:

$ cut -b 1,2,3 content.txt 
Ubu
Mic
OsX
Uni
Fre

我們也可以用如下命令列出一個範圍;

$ cut -b 1-3,5-7 content.txt 
Ubutu 
Micoso
OsXEl 
Uni
FreBSD

一些實用的例子

cut 是一個實用的命令,常常和其他Linux或Unix命令結合使用 。

例如如果你想提取 ps 命令中的 USER,PID和COMMAND:

ps -L u n | tr -s " " | cut -d " " -f 2,3,14-
USER PID COMMAND
0 676 /sbin/agetty -o -p -- \u --keep-baud 115200,38400,9600 ttyS0 vt220
0 681 /sbin/agetty -o -p -- \u --noclear tty1 linux
0 23174 -bash
0 26737 ps -L u n
0 26738 tr -s
0 26739 cut -d -f 2,3,14-

再測試一個例子,提取記憶體的 total,used和free值,並儲存到一個檔案中。

$ free -m | tr -s ' ' | sed '/^Mem/!d' | cut -d" " -f2-4 >> memory.txt
$ cat memory.txt
985 86 234

總結

cut 命令可以和很多其他Linux或Unix命令通過管道連線。可以通過管道傳遞一個或多個過濾器進行額外的文字處理。

cut 命令的侷限性之一是它不支援指定多個字元作為分隔符。多個空格會被計算為多個欄位分隔符,因此必須在 cut 命令前使用 tr 命令才能獲得需要的輸出。


公眾號:良許Linux

有收穫?希望老鐵們來個三連擊,給更多的人看到這篇文章