一些命令行小技巧:wc、sort、sed 和 tr
Linux 發(fā)行版十分好用,而且它們有一些用戶可能不知道的技巧。讓我們來看看一些命令行實用工具,當(dāng)你熱衷于終端而不是 GUI 時,它們可能更順手。
我們都知道在一個系統(tǒng)上使用終端會更高效。當(dāng)你編輯和排版一個文本文件時,終端會讓你確切的感受到,生活如此簡單。
本文將向你介紹 wc、sort、tr 和 sed 命令。
1. wc
wc 是一個實用工具,全稱是 “word count”。顧名思義,它可以用來統(tǒng)計任何文件的行數(shù)、單詞數(shù)和字節(jié)數(shù)。
讓我們來看看它是如何工作的:
- $ wc filename
- lines words characters filename
輸出的是文件的行數(shù)、單詞數(shù)、字符數(shù)和文件名。
想獲得特定的輸出,我們必須使用選項:
- -c 打印字節(jié)總數(shù)
- -l 打印行數(shù)
- -w 打印單詞總數(shù)
- -m 打印字符總數(shù)
wc 示例
讓我們來看看它的運(yùn)行結(jié)果。
讓我們從一個文本文件 lormipsm.txt 開始。首先,我們通過 cat 查看文件內(nèi)容,然后使用 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
假設(shè)我只想查看文件的字節(jié)數(shù):
- $ wc -c loremipsm.txt
- 268 loremipsm.txt
查看文件的行數(shù):
- $ wc -l loremipsm.txt
- 3 loremipsm.txt
查看文件的單詞數(shù):
- $ wc -w loremipsm.txt
- 41 loremipsm.txt
現(xiàn)在只查看文件的字符數(shù):
- $ wc -m loremipsm.txt
- 268 loremipsm.txt
2. sort
sort 命令是最有用的工具之一。它會對文件的數(shù)據(jù)進(jìn)行排序??梢愿鶕?jù)字符或數(shù)字進(jìn)行升序或降序排列。它也可以用來對文件中的行進(jìn)行排序和隨機(jī)化。
使用 sort 非常簡單。我們需要做的僅僅是提供一個文件名:
- $ sort filename
默認(rèn)的是按照字母順序?qū)?shù)據(jù)進(jìn)行排序。需要注意的是 sort 命令僅僅是對數(shù)據(jù)進(jìn)行排序展示。它并不會改寫文件。
使用 sort 命令的一些有用的選項:
- -r 將文件中的行按倒序進(jìn)行排序
- -R 將文件中的行打亂為隨機(jī)順序
- -o 將輸出保存到另一個文件中
- -k 按照特定列進(jìn)行排序
- -t 使用指定的分隔符,而不使用空格
- -n 根據(jù)數(shù)值對數(shù)據(jù)進(jìn)行排序
sort 示例
讓我們看看 sort 的幾個簡單示例。
我們有一個 list.txt 的文件,包含逗號分隔的名稱和數(shù)值。
首先讓我們打印出文件內(nèi)容并簡單排序:
- $ 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
現(xiàn)在對數(shù)據(jù)進(jìn)行倒序排序:
- $ 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
讓我們打亂數(shù)據(jù):
- $ 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
來看一點(diǎn)更復(fù)雜的。這次我們根據(jù)第二個字段,也就是數(shù)值對數(shù)據(jù)進(jìn)行排序,并使用 -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 選項按數(shù)字順序進(jìn)行排序,-k 選項用來指定要排序的字段(在本例中為第 2 個字段),-t 選項指定分隔符或字段分隔符(逗號),-o 選項將輸出保存到 sorted_list.txt 文件中。
3. sed
sed 是一個流編輯器,用于過濾和轉(zhuǎn)換輸出中的文本。這意味著我們不需要對原文件進(jìn)行修改,只需要對輸出進(jìn)行修改。如果需要,我們可以將更改保存到一個新的文件中。sed 提供了很多有用的選項用于過濾和編輯數(shù)據(jù)。
sed 的語法格式如下:
- $ sed [OPTION] ‘PATTERN’ filename
sed 常用的一些選項:
- -n 取消默認(rèn)輸出
- p 打印指定的數(shù)據(jù)
- d 刪除指定行
- q 退出 sed 腳本
sed 示例
我們來看看 sed 是如何運(yùn)作的。我們從 data 文件開始,其中的字段表示編號、名稱、年齡和操作系統(tǒng)。
如果行出現(xiàn)在特定的行范圍內(nèi),該行將打印 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 打印出符合匹配規(guī)則的數(shù)據(jù)。sed 的默認(rèn)行為是在解析后打印每一行。這意味著由于使用了 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
從文件中搜索特定的關(guān)鍵字:
- $ 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
在這些例子中,我們在 / / 中使用了一個正則表達(dá)式。如果文件中有類似的單詞,但大小寫不一致,可以使用 I 使得搜索不區(qū)分大小寫?;叵胍幌?,-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/ / / 表示它是一個正則表達(dá)式。在兩個 / 之間的就是定位的單詞和需要替換的新單詞。
4. tr
tr 命令可以用來轉(zhuǎn)換或刪除字符。它可以將小寫字母轉(zhuǎn)換為大寫字母,也可以將大寫字母轉(zhuǎn)換為小寫字母,可以消除重復(fù)字符,也可以刪除特定字符。
tr 的奇怪之處在于,它不同于 wc、sort、sed 那樣接受文件作為輸入。我們使用 | (管道符)為 tr 命令提供輸入。
- $ cat filename | tr [OPTION]
tr 命令使用的一些選項:
- -d 刪除給定輸入第一個集合中的指定字符,不做轉(zhuǎn)換
- -s 將重復(fù)出現(xiàn)的字符替換為單個
tr 示例
現(xiàn)在讓我們使用 tr 命令將 letter 文件中的所有小寫字符轉(zhuǎn)換為大寫字符:
- $ 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 范圍內(nèi)的小寫字符轉(zhuǎn)換為大寫字符。
刪除文件中的 o 字符:
- $ cat letter | tr -d 'o'
- Linux is t easy t learn,
- And yu shuld try it t.
從文件中壓縮字符 o 意味著如果 o 在文件中重復(fù)出現(xiàn),那么它將會被刪除并且只打印一次:
- $ cat letter | tr -s 'o'
- Linux is to easy to learn,
- And you should try it to.
總結(jié)
這是使用 wc、sort、sed、tr 命令的快速演示。這些命令可以方便快捷的操作終端上的文本文件。你可以使用 man 命令來了解這些命令的更多信息。