一些命令行小技巧:wc、sort、sed 和 tr
Linux 發行版十分好用,而且它們有一些用戶可能不知道的技巧。讓我們來看看一些命令行實用工具,當你熱衷於終端而不是 GUI 時,它們可能更順手。
我們都知道在一個系統上使用終端會更高效。當你編輯和排版一個文本文件時,終端會讓你確切的感受到,生活如此簡單。
本文將向你介紹 wc、sort、tr 和 sed 命令。
wc
wc 是一個實用工具,全稱是 「word count」。顧名思義,它可以用來統計任何文件的行數、單詞數和位元組數。
讓我們來看看它是如何工作的:
$ wc filename
lines words characters filename
輸出的是文件的行數、單詞數、字元數和文件名。
想獲得特定的輸出,我們必須使用選項:
-c列印位元組總數-l列印行數-w列印單詞總數-m列印字元總數
wc 示例
讓我們來看看它的運行結果。
讓我們從一個文本文件 lormipsm.txt 開始。首先,我們通過 cat 查看文件內容,然後使用 wc:
$ cat loremipsm.txt
Linux is the best-known and most-used open source operating system.
As an operating system, Linux is software that sits underneath all of the other software on a computer,
receiving requests from those programs and replaying these requests to the computer's hardware.
$ wc loremipsm.txt
3 41 268 loremipsm.txt
假設我只想查看文件的位元組數:
$ wc -c loremipsm.txt
268 loremipsm.txt
查看文件的行數:
$ wc -l loremipsm.txt
3 loremipsm.txt
查看文件的單詞數:
$ wc -w loremipsm.txt
41 loremipsm.txt
現在只查看文件的字元數:
$ wc -m loremipsm.txt
268 loremipsm.txt
sort
sort 命令是最有用的工具之一。它會對文件的數據進行排序。可以根據字元或數字進行升序或降序排列。它也可以用來對文件中的行進行排序和隨機化。
使用 sort 非常簡單。我們需要做的僅僅是提供一個文件名:
$ sort filename
默認的是按照字母順序對數據進行排序。需要注意的是 sort 命令僅僅是對數據進行排序展示。它並不會改寫文件。
使用 sort 命令的一些有用的選項:
-r將文件中的行按倒序進行排序-R將文件中的行打亂為隨機順序-o將輸出保存到另一個文件中-k按照特定列進行排序-t使用指定的分隔符,而不使用空格-n根據數值對數據進行排序
sort 示例
讓我們看看 sort 的幾個簡單示例。
我們有一個 list.txt 的文件,包含逗號分隔的名稱和數值。
首先讓我們列印出文件內容並簡單排序:
$ cat list.txt
Cieran Wilks, 9
Adelina Rowland, 4
Hayden Mcfarlnd, 1
Ananya Lamb, 5
Shyam Head, 2
Lauryn Fuents, 8
Kristian Felix, 10
Ruden Dyer, 3
Greyson Meyers, 6
Luther Cooke, 7
$ sort list.txt
Adelina Rowland, 4
Ananya Lamb, 5
Cieran Wilks, 9
Greyson Meyers, 6
Hayden Mcfarlnd, 1
Kristian Felix, 10
Lauryn Fuents, 8
Luther Cooke, 7
Ruden Dyer, 3
Shyam Head, 2
現在對數據進行倒序排序:
$ sort -r list.txt
Shyam Head, 2
Ruden Dyer, 3
Luther Cooke, 7
Lauryn Fuents, 8
Kristian Felix, 10
Hayden Mcfarlnd, 1
Greyson Meyers, 6
Cieran Wilks, 9
Ananya Lamb, 5
Adelina Rowland, 4
讓我們打亂數據:
$ sort -R list.txt
Cieran Wilks, 9
Greyson Meyers, 6
Adelina Rowland, 4
Kristian Felix, 10
Luther Cooke, 7
Ruden Dyer, 3
Lauryn Fuents, 8
Hayden Mcfarlnd, 1
Ananya Lamb, 5
Shyam Head, 2
來看一點更複雜的。這次我們根據第二個欄位,也就是數值對數據進行排序,並使用 -o 選項將輸出保存到另一個文件中:
$ sort -n -k2 -t ',' -o sorted_list.txt list.txt
$ ls
sorted_list.txt list.txt
$ cat sorted_list.txt
Hayden Mcfarlnd, 1
Shyam Head, 2
Ruden Dyer, 3
Adelina Rowland, 4
Ananya Lamb, 5
Greyson Meyers, 6
Luther Cooke, 7
Lauryn Fuents, 8
Cieran Wilks, 9
Kristian Felix, 10
這裡我們使用 -n 選項按數字順序進行排序,-k 選項用來指定要排序的欄位(在本例中為第 2 個欄位),-t 選項指定分隔符或欄位分隔符(逗號),-o 選項將輸出保存到 sorted_list.txt 文件中。
sed
sed 是一個流編輯器,用於過濾和轉換輸出中的文本。這意味著我們不需要對原文件進行修改,只需要對輸出進行修改。如果需要,我們可以將更改保存到一個新的文件中。sed 提供了很多有用的選項用於過濾和編輯數據。
sed 的語法格式如下:
$ sed [OPTION] 『PATTERN』 filename
sed 常用的一些選項:
-n取消默認輸出p列印指定的數據d刪除指定行q退出sed腳本
sed 示例
我們來看看 sed 是如何運作的。我們從 data 文件開始,其中的欄位表示編號、名稱、年齡和操作系統。
如果行出現在特定的行範圍內,該行將列印 2 次:
$ cat data
1 Vicky Grant 20 linux
2 Nora Burton 19 Mac
3 Willis Castillo 21 Windows
4 Gilberto Mack 30 Windows
5 Aubrey Hayes 17 windows
6 Allan Snyder 21 mac
7 Freddie Dean 25 linux
8 Ralph Martin 19 linux
9 Mindy Howard 20 Mac
$ sed '3,7 p' data
1 Vicky Grant 20 linux
2 Nora Burton 19 Mac
3 Willis Castillo 21 Windows
3 Willis Castillo 21 Windows
4 Gilberto Mack 30 Windows
4 Gilberto Mack 30 Windows
5 Aubrey Hayes 17 windows
5 Aubrey Hayes 17 windows
6 Allan Snyder 21 mac
6 Allan Snyder 21 mac
7 Freddie Dean 25 linux
7 Freddie Dean 25 linux
8 Ralph Martin 19 linux
9 Mindy Howard 20 Mac
這裡的操作用單引號括起來,表示第 3 行和第 7 行,並且使用了 p 列印出符合匹配規則的數據。sed 的默認行為是在解析後列印每一行。這意味著由於使用了 p ,第 3 行到第 7 行列印了兩次。
如何列印文件中特定的行?使用 -n 選項來消除在輸出中不匹配的行:
$ sed -n '3,7 p' data
3 Willis Castillo 21 Windows
4 Gilberto Mack 30 Windows
5 Aubrey Hayes 17 windows
6 Allan Snyder 21 mac
7 Freddie Dean 25 linux
使用 『-n』 僅僅只有第 3 行到第 7 行會被列印。
省略文件中的特定行。使用 d 從輸出中刪除行:
$ sed '3 d' data
1 Vicky Grant 20 linux
2 Nora Burton 19 Mac
4 Gilberto Mack 30 Windows
5 Aubrey Hayes 17 windows
6 Allan Snyder 21 mac
7 Freddie Dean 25 linux
8 Ralph Martin 19 linux
9 Mindy Howard 20 Mac
$ sed '5,9 d' data
1 Vicky Grant 20 linux
2 Nora Burton 19 Mac
3 Willis Castillo 21 Windows
4 Gilberto Mack 30 Windows
從文件中搜索特定的關鍵字:
$ sed -n '/linux/ p' data
7 Freddie Dean 25 linux
8 Ralph Martin 19 linux
$ sed -n '/linux/I p' data
1 Vicky Grant 20 Linux
7 Freddie Dean 25 linux
8 Ralph Martin 19 linux
在這些例子中,我們在 / / 中使用了一個正則表達式。如果文件中有類似的單詞,但大小寫不一致,可以使用 I 使得搜索不區分大小寫。回想一下,-n 刪除了輸出中不匹配的行。
替換文件中的單詞:
$ sed 's/linux/linus/' data
1 Vicky Grant 20 Linux
2 Nora Burton 19 Mac
3 Willis Castillo 21 Windows
4 Gilberto Mack 30 Windows
5 Aubrey Hayes 17 windows
6 Allan Snyder 21 mac
7 Freddie Dean 25 linus
8 Ralph Martin 19 linus
9 Mindy Howard 20 Mac
這裡 s/ / / 表示它是一個正則表達式。在兩個 / 之間的就是定位的單詞和需要替換的新單詞。
tr
tr 命令可以用來轉換或刪除字元。它可以將小寫字母轉換為大寫字母,也可以將大寫字母轉換為小寫字母,可以消除重複字元,也可以刪除特定字元。
tr 的奇怪之處在於,它不同於 wc、sort、sed 那樣接受文件作為輸入。我們使用 | (管道符)為 tr 命令提供輸入。
$ cat filename | tr [OPTION]
tr 命令使用的一些選項:
-d刪除給定輸入第一個集合中的指定字元,不做轉換-s將重複出現的字元替換為單個
tr 示例
現在讓我們使用 tr 命令將 letter 文件中的所有小寫字元轉換為大寫字元:
$ cat letter
Linux is too easy to learn,
And you should try it too.
$ cat letter | tr 'a-z' 'A-Z'
LINUX IS TOO EASY TO LEARN,
AND YOU SHOULD TRY IT TOO.
這裡的 a-z、A-Z 表示我們想要將 a 到 z 範圍內的小寫字元轉換為大寫字元。
刪除文件中的 o 字元:
$ cat letter | tr -d 'o'
Linux is t easy t learn,
And yu shuld try it t.
從文件中壓縮字元 o 意味著如果 o 在文件中重複出現,那麼它將會被刪除並且只列印一次:
$ cat letter | tr -s 'o'
Linux is to easy to learn,
And you should try it to.
總結
這是使用 wc、sort、sed、tr 命令的快速演示。這些命令可以方便快捷的操作終端上的文本文件。你可以使用 man 命令來了解這些命令的更多信息。
via: https://fedoramagazine.org/command-line-quick-tips-wc-sort-sed-and-tr/
作者:mahesh1b 選題:lujun9972 譯者:perfiffer 校對:wxy
本文轉載來自 Linux 中國: https://github.com/Linux-CN/archive

















