1. 寫在前面
本文主要介紹:Linux "cut "和 “tr” 命令行實用程序概述;
公眾號: 滑翔的紙飛機
2. Linux 命令: cut
“cut” 命令是一種命令行工具,允許我們剪切指定文件或管道數據的部分內容,并將結果打印到標準輸出。
root@dev:~# man cut
-------------------------------------------------------
NAMEcut - remove sections from each line of files
SYNOPSIScut OPTION... [FILE]...
... ...
-b, --bytes=LISTselect only these bytes
-c, --characters=LISTselect only these characters
-d, --delimiter=DELIMuse DELIM instead of TAB for field delimiter
... ...
下面是一個文本文件:讓我們看看如何操作下面的文本文件,以根據需要打印輸出。
test.txt:
Nov 15 00:13:08 dev com.apple.xpc.launchd[1] (com.apple.mdworker.shared.10000000-0000-0000-0000-000000000000[1938]): Service exited due to SIGKILL | sent by mds[98]
Nov 15 00:13:15 dev com.apple.xpc.launchd[1] (com.apple.mdworker.shared.01000000-0000-0000-0000-000000000000[1936]): Service exited due to SIGKILL | sent by mds[98]
Nov 15 00:13:15 dev com.apple.xpc.launchd[1] (com.apple.mdworker.shared.06000000-0200-0000-0000-000000000000[1935]): Service exited due to SIGKILL | sent by mds[98]
Nov 15 00:13:15 dev com.apple.xpc.launchd[1] (com.apple.mdworker.shared.04000000-0200-0000-0000-000000000000[1939]): Service exited due to SIGKILL | sent by mds[98]
Nov 15 00:13:41 dev com.apple.xpc.launchd[1] (com.apple.mdworker.shared.05000000-0600-0000-0000-000000000000[1940]): Service exited due to SIGKILL | sent by mds[98]
Nov 15 00:13:41 dev com.apple.xpc.launchd[1] (com.apple.mdworker.shared.08000000-0100-0000-0000-000000000000[1941]): Service exited due to SIGKILL | sent by mds[98]
Nov 15 00:13:41 dev com.apple.xpc.launchd[1] (com.apple.mdworker.shared.0D000000-0200-0000-0000-000000000000[1917]): Service exited due to SIGKILL | sent by mds[98]
Nov 15 00:13:41 dev com.apple.xpc.launchd[1] (com.apple.mdworker.shared.0E000000-0400-0000-0000-000000000000[1937]): Service exited due to SIGKILL | sent by mds[98]
2.1 按字符范圍打印
在一定字符范圍內打印輸出 :
范圍:1 - 5
root@dev:~# cut -c 1-5 test.txt
-------------------------------------------------------
Nov 1
Nov 1
Nov 1
Nov 1
Nov 1
Nov 1
Nov 1
Nov 1
范圍:21 - 40
root@dev:~# cut -c 21-40 test.txt
-------------------------------------------------------
com.apple.xpc.launch
com.apple.xpc.launch
com.apple.xpc.launch
com.apple.xpc.launch
com.apple.xpc.launch
com.apple.xpc.launch
com.apple.xpc.launch
com.apple.xpc.launch
范圍:70 - end
root@dev:~# cut -c 76- test.txt
-------------------------------------------------------
00000-0000-0000-0000-000000000000[1938]): Service exited due to SIGKILL | sent by mds[98]
00000-0000-0000-0000-000000000000[1936]): Service exited due to SIGKILL | sent by mds[98]
00000-0200-0000-0000-000000000000[1935]): Service exited due to SIGKILL | sent by mds[98]
00000-0200-0000-0000-000000000000[1939]): Service exited due to SIGKILL | sent by mds[98]
00000-0600-0000-0000-000000000000[1940]): Service exited due to SIGKILL | sent by mds[98]
00000-0100-0000-0000-000000000000[1941]): Service exited due to SIGKILL | sent by mds[98]
00000-0200-0000-0000-000000000000[1917]): Service exited due to SIGKILL | sent by mds[98]
00000-0400-0000-0000-000000000000[1937]): Service exited due to SIGKILL | sent by mds[98]
2.2 按字段名打印
假設我們想根據字段從以下文件中提取數據。
test.txt:
NAME EMAIL PHONE ADDRESS
devid devid@text.com 0897663232 beijin,china
harry harry@text.com 0232323232 hangzhou,china
jane jane@text.com 0323213122 zhejiang,china
我們必須使用"-d = delimiter"
選項(可以是一個字符,默認為 TAB)來分隔每個字段。然后,我們必須指定要打印的字段編號。
-d, --delimiter=DELIM
-f, --fields=LIST >> cut -d ' ' -f1
在下面的演示中,我們使用空格(’ ')作為分隔符。
# 打印空格分割第1列
root@dev:~# cut -d ' ' -f1 test.txt
-------------------------------------------------------
NAME
devid
harry
jane# 打印空格分割第2列
root@dev:~# cut -d ' ' -f2 test.txt
-------------------------------------------------------
EMAIL
devid@text.com
harry@text.com
jane@text.com
打印多個字段:打印第1、3列
root@jpzhang-dev:~# cut -d ' ' -f1,3 test.txt
-------------------------------------------------------
NAME PHONE
devid 0897663232
harry 0232323232
jane 0323213122
使用逗號 (, ) 作為分隔符:
root@dev:~# echo "jane,jane@dev,12345678,china" | cut -d ',' -f1
--------------------------------------------------------------------
janeroot@dev:~# echo "jane,jane@dev,12345678,china" | cut -d ',' -f2
--------------------------------------------------------------------
jane@devroot@dev:~# echo "jane,jane@dev,12345678,china" | cut -d ',' -f3
--------------------------------------------------------------------12345678
3. Linux 命令: tr
Linux tr 命令用于轉換或刪除文件中的字符。
tr 指令從標準輸入設備讀取數據,經過字符串轉譯后,將結果輸出到標準輸出設備。
語法
tr [-cdst][--help][--version][第一字符集][第二字符集]
tr [OPTION]…SET1[SET2]
具體參數:
>> man tr
--------------------------------------------------------------------
tr [OPTION]... SET1 [SET2]
# Options
-c, -C, --complementuse the complement of SET1
-d, --deletedelete characters in SET1, do not translate
-s, --squeeze-repeatsreplace each sequence of a repeated character that is listed in the last specified SET, with a single occurrence of that character
-t, --truncate-set1first truncate SET1 to length of SET2
...
...
參數說明:
? -c, --complement:反選設定字符。也就是符合 SET1 的部份不做處理,不符合的剩余部份才進行轉換
? -d, --delete:刪除指令字符
? -s, --squeeze-repeats:縮減連續重復的字符成指定的單個字符
? -t, --truncate-set1:削減 SET1 指定范圍,使之與 SET2 設定長度相等
? --help:顯示程序用法信息
? --version:顯示程序本身的版本信息
字符集合范圍:
? \NNN 八進制值的字符 NNN (1 to 3 為八進制值的字符)
? \\ 反斜杠
? \a Ctrl-G 鈴聲
? \b Ctrl-H 退格符
? \f Ctrl-L 走行換頁
? \n Ctrl-J 新行
? \r Ctrl-M 回車
? \t Ctrl-I tab鍵
? \v Ctrl-X 水平制表符
? CHAR1-CHAR2 :字符范圍從 CHAR1 到 CHAR2 的指定,范圍的指定以 ASCII 碼的次序為基礎,只能由小到大,不能由大到小。
? [CHAR*] :這是 SET2 專用的設定,功能是重復指定的字符到與 SET1 相同長度為止
? [CHAR*REPEAT] :這也是 SET2 專用的設定,功能是重復指定的字符到設定的 REPEAT 次數為止(REPEAT 的數字采 8 進位制計算,以 0 為開始)
? [:alnum:] :所有字母字符與數字
? [:alpha:] :所有字母字符
? [:blank:] :所有水平空格
? [:cntrl:] :所有控制字符
? [:digit:] :所有數字
? [:graph:] :所有可打印的字符(不包含空格符)
? [:lower:] :所有小寫字母
? [:print:] :所有可打印的字符(包含空格符)
? [:punct:] :所有標點字符
? [:space:] :所有水平與垂直空格符
? [:upper:] :所有大寫字母
? [:xdigit:] :所有 16 進位制的數字
? [=CHAR=] :所有符合指定的字符(等號里的 CHAR,代表你可自訂的字符)
3.1 替換字符
替換字符:‘H’ > ‘h’
root@dev:~# echo "Hello World" | tr 'H' 'h'
--------------------------------------------------------------------
hello World
替換字符:‘Ho’ > ‘xx’ 即 ‘H’ 或 ‘o’ 替換為 ‘x’
root@dev:~# echo "Hello World" | tr 'Ho' 'xx'
--------------------------------------------------------------------
xellx Wxrld
3.2 刪除字符
# 刪除 'H' 或 'o'
root@dev:~# echo "Hello World" | tr -d 'Ho'
--------------------------------------------------------------------
ell Wrld# 反選,除'Hd\n'其他刪除
root@dev:~# echo "Hello World" | tr -cd 'Hd\n'
--------------------------------------------------------------------
Hd# 反選,除數字外其他刪除
root@dev:~# echo "Hello World 12345 " | tr -cd [:digit:]
--------------------------------------------------------------------
12345# 反選,除字母外其他刪除
root@dev:~# echo "Hello World 12345 " | tr -cd [:alpha:]
--------------------------------------------------------------------
HelloWorld
3.3 壓縮字符
# 壓縮指定重復字符
root@dev:~# echo "HHHHHHHHellooooo Woooorrrrrrrrrldddddddddddddddddd" | tr -s 'Hord'
------------------------------------------------------------------------------------
Hello World
# 壓縮重復字符,小寫轉換大寫
root@dev:~# echo "Hello World" | tr -s [:lower:] [:upper:]
------------------------------------------------------------------------------------
HELO WORLD