在這篇文章中,我們將會(huì)看一些 Linux 中的過濾器命令行工具。過濾器是一個(gè)程序,它從標(biāo)準(zhǔn)輸入讀取數(shù)據(jù),在數(shù)據(jù)上執(zhí)行操作,然后把結(jié)果寫到標(biāo)準(zhǔn)輸出。
編譯自: http://www./linux-file-operations-commands/ 作者: Aaron Kili 譯者: yangmingming
在這篇文章中,我們將會(huì)看一些 Linux 中的過濾器命令行工具。過濾器是一個(gè)程序,它從標(biāo)準(zhǔn)輸入讀取數(shù)據(jù),在數(shù)據(jù)上執(zhí)行操作,然后把結(jié)果寫到標(biāo)準(zhǔn)輸出。
因此,它可以用來以強(qiáng)大的方式處理信息,例如重新結(jié)構(gòu)化輸出以生成有用的報(bào)告,修改文件里面的文本,以及其他很多系統(tǒng)管理任務(wù)。
下面是 Linux 上的一些有用的文件或者文本過濾器。
1、 awk 命令
awk [1] 是一個(gè)卓越的模式掃描和處理語(yǔ)言,它可被用于在 Linux 下構(gòu)造有用的過濾器。你可以通過閱讀我們的 awk 系列 1 到 13 部分 [2] 來開始使用它。
另外,也可以通過閱讀 awk 的 man 手冊(cè)來獲取更多的信息和使用選項(xiàng)。
2、 sed 命令
sed [3] 是一款過濾和轉(zhuǎn)換文本的強(qiáng)大的流編輯器。我們已經(jīng)寫了兩篇關(guān)于 sed 的有用的文章,你可以通過這兒來了解:
sed 的 man 手冊(cè)已經(jīng)添加控制選項(xiàng)和說明:
3、 grep、 egrep、 fgrep、 rgrep 命令行
這些過濾器輸出匹配指定模式的行。它們從一個(gè)文件或者標(biāo)準(zhǔn)輸入讀取行,并且輸出所有匹配的行,默認(rèn)輸出到標(biāo)準(zhǔn)輸出。
注意 :主程序是 grep [6] ,這些變體與使用特定的選項(xiàng)的 grep [7] 相同,如下所示(為了向后兼容性,它們依舊在使用):
$ egrep = grep - E
$ fgrep = grep - F
$ rgrep = grep - r
下面是一些基本的 grep 命令:
tecmint@TecMint ~ $ grep 'aaronkilik' / etc / passwd
aaronkilik : x : 1001 : 1001 :: /home/ aaronkilik :
tecmint@TecMint ~ $ cat / etc / passwd | grep 'aronkilik'
aaronkilik : x : 1001 : 1001 :: /home/ aaronkilik :
在 Linux 下的 grep、 egrep 和 fgrep 的差異? [8] 中,你可以了解更多。
4、 head 命令
head 用于顯示文件前面的部分,默認(rèn)情況下它輸出前 10 行。你可以使用 -n 標(biāo)志來指定顯示的行數(shù):
tecmint@TecMint ~ $ head / var / log / auth . log
Jan 2 10 : 45 : 01 TecMint CRON [ 3383 ]: pam_unix ( cron : session ): session opened for user root by ( uid = 0 )
Jan 2 10 : 45 : 01 TecMint CRON [ 3383 ]: pam_unix ( cron : session ): session closed for user root
Jan 2 10 : 51 : 34 TecMint sudo : tecmint : TTY = unknown ; PWD = /home/ tecmint ; USER = root ; COMMAND = /usr/ lib / linuxmint / mintUpdate / checkAPT . py
Jan 2 10 : 51 : 34 TecMint sudo : pam_unix ( sudo : session ): session opened for user root by ( uid = 0 )
Jan 2 10 : 51 : 39 TecMint sudo : pam_unix ( sudo : session ): session closed for user root
Jan 2 10 : 55 : 01 TecMint CRON [ 4099 ]: pam_unix ( cron : session ): session opened for user root by ( uid = 0 )
Jan 2 10 : 55 : 01 TecMint CRON [ 4099 ]: pam_unix ( cron : session ): session closed for user root
Jan 2 11 : 05 : 01 TecMint CRON [ 4138 ]: pam_unix ( cron : session ): session opened for user root by ( uid = 0 )
Jan 2 11 : 05 : 01 TecMint CRON [ 4138 ]: pam_unix ( cron : session ): session closed for user root
Jan 2 11 : 09 : 01 TecMint CRON [ 4146 ]: pam_unix ( cron : session ): session opened for user root by ( uid = 0 )
tecmint@TecMint ~ $ head - n 5 / var / log / auth . log
Jan 2 10 : 45 : 01 TecMint CRON [ 3383 ]: pam_unix ( cron : session ): session opened for user root by ( uid = 0 )
Jan 2 10 : 45 : 01 TecMint CRON [ 3383 ]: pam_unix ( cron : session ): session closed for user root
Jan 2 10 : 51 : 34 TecMint sudo : tecmint : TTY = unknown ; PWD = /home/ tecmint ; USER = root ; COMMAND = /usr/ lib / linuxmint / mintUpdate / checkAPT . py
Jan 2 10 : 51 : 34 TecMint sudo : pam_unix ( sudo : session ): session opened for user root by ( uid = 0 )
Jan 2 10 : 51 : 39 TecMint sudo : pam_unix ( sudo : session ): session closed for user root
學(xué)習(xí)如何 使用帶有 tail 和 cat 命令的 head 命令 [9] ,以便在 Linux 下更有效的使用。
5、 tail 命令
tail 輸出一個(gè)文件的后面的部分(默認(rèn) 10 行)。使用 -n 選項(xiàng)來指定顯示的行數(shù)。
下面的命令將會(huì)輸出指定文件的最后 5 行:
tecmint@TecMint ~ $ tail - n 5 / var / log / auth . log
Jan 6 13 : 01 : 27 TecMint sshd [ 1269 ]: Server listening on 0.0 . 0.0 port 22.
Jan 6 13 : 01 : 27 TecMint sshd [ 1269 ]: Server listening on :: port 22.
Jan 6 13 : 01 : 27 TecMint sshd [ 1269 ]: Received SIGHUP ; restarting .
Jan 6 13 : 01 : 27 TecMint sshd [ 1269 ]: Server listening on 0.0 . 0.0 port 22.
Jan 6 13 : 01 : 27 TecMint sshd [ 1269 ]: Server listening on :: port 22.
另外,tail 有一個(gè)特殊的選項(xiàng) -f ,可以 實(shí)時(shí)查看一個(gè)文件的變化 [10] (尤其是日志文件)。
下面的命令將會(huì)使你能夠監(jiān)控指定文件的變化:
tecmint@TecMint ~ $ tail - f / var / log / auth . log
Jan 6 12 : 58 : 01 TecMint sshd [ 1269 ]: Server listening on :: port 22.
Jan 6 12 : 58 : 11 TecMint sshd [ 1269 ]: Received SIGHUP ; restarting .
Jan 6 12 : 58 : 12 TecMint sshd [ 1269 ]: Server listening on 0.0 . 0.0 port 22.
Jan 6 12 : 58 : 12 TecMint sshd [ 1269 ]: Server listening on :: port 22.
Jan 6 13 : 01 : 27 TecMint sshd [ 1269 ]: Received SIGHUP ; restarting .
Jan 6 13 : 01 : 27 TecMint sshd [ 1269 ]: Server listening on 0.0 . 0.0 port 22.
Jan 6 13 : 01 : 27 TecMint sshd [ 1269 ]: Server listening on :: port 22.
Jan 6 13 : 01 : 27 TecMint sshd [ 1269 ]: Received SIGHUP ; restarting .
Jan 6 13 : 01 : 27 TecMint sshd [ 1269 ]: Server listening on 0.0 . 0.0 port 22.
Jan 6 13 : 01 : 27 TecMint sshd [ 1269 ]: Server listening on :: port 22.
閱讀 tail 的 man 手冊(cè),獲取使用選項(xiàng)和說明的完整內(nèi)容:
6、 sort 命令
sort [11] 用于將文本文件或標(biāo)準(zhǔn)輸入的行進(jìn)行排序。
下面是一個(gè)名為 domain.list 的文件的內(nèi)容:
tecmint@TecMint ~ $ cat domains . list
tecmint . com
tecmint . com
news . tecmint . com
news . tecmint . com
linuxsay . com
linuxsay . com
windowsmint . com
windowsmint . com
你可以像這樣運(yùn)行一個(gè)簡(jiǎn)單的 sort 命令 [12] 來排序文件內(nèi)容:
tecmint@TecMint ~ $ sort domains . list
linuxsay . com
linuxsay . com
news . tecmint . com
news . tecmint . com
tecmint . com
tecmint . com
windowsmint . com
windowsmint . com
你可以有多種方式來使用 sort 命令,請(qǐng)參閱以下一些關(guān)于 sort 命令的有用的文章。
Linux 的 ‘sort’命令的14個(gè)有用的范例(一) [13]
Linux 的 'sort'命令的七個(gè)有趣實(shí)例(二) [14]
如何基于修改日期和時(shí)間來查找和排序文件 [15]
7、 uniq 命令
uniq [16] 命令用于報(bào)告或者忽略重復(fù)行,它從標(biāo)準(zhǔn)輸入過濾行,并且把結(jié)果寫到標(biāo)準(zhǔn)輸出。
在對(duì)一個(gè)輸入流運(yùn)行 sort 之后,你可以使用 uniq 刪除重復(fù)行,如下例所示。
為了顯示行出現(xiàn)的數(shù)目,使用 -c 選項(xiàng),要在對(duì)比時(shí)忽略大小寫的差異,使用 -i 選項(xiàng):
tecmint@TecMint ~ $ cat domains . list
tecmint . com
tecmint . com
news . tecmint . com
news . tecmint . com
linuxsay . com
linuxsay . com
windowsmint . com
tecmint@TecMint ~ $ sort domains . list | uniq - c
2 linuxsay . com
2 news . tecmint . com
2 tecmint . com
1 windowsmint . com
通過閱讀 uniq 的 man 手冊(cè)來獲取進(jìn)一步的使用信息和選項(xiàng):
8、 fmt 命令行
fmt 是一款簡(jiǎn)單的優(yōu)化的文本格式化器,它重新格式化指定文件的段落,并且打印結(jié)果到標(biāo)準(zhǔn)輸出。
以下是從文件 domain-list.txt 提取的內(nèi)容:
1.tecmint . com 2.news . tecmint . com 3.linuxsay . com 4.windowsmint . com
為了把上面的內(nèi)容重新格式化成一個(gè)標(biāo)準(zhǔn)的清單,運(yùn)行下面的命令,使用 -w 選項(xiàng)定義最大行寬度:
tecmint@TecMint ~ $ cat domain - list . txt
1.tecmint . com 2.news . tecmint . com 3.linuxsay . com 4.windowsmint . com
tecmint@TecMint ~ $ fmt - w 1 domain - list . txt
1.tecmint . com
2.news . tecmint . com
3.linuxsay . com
4.windowsmint . com
9、 pr 命令
pr 命令轉(zhuǎn)換文本文件或者標(biāo)準(zhǔn)輸入之后打印出來。例如在 Debian 系統(tǒng)上,你可以像下面這樣顯示所有的安裝包:
為了將要打印的列表在頁(yè)面和列中組織好,使用以下命令。
tecmint@TecMint ~ $ dpkg - l | pr -- columns 3 - l 20
2017 - 01 - 06 13 : 19 Page 1
Desired = Unknown / Install ii adduser ii apg
| Status = Not / Inst / Conf - ii adwaita - icon - theme ii app - install - data
| / Err?=(none)/ Reinst - r ii adwaita - icon - theme - ii apparmor
||/ Name ii alsa - base ii apt
-=================== ii alsa - utils ii apt - clone
ii accountsservice ii anacron ii apt - transport - https
ii acl ii apache2 ii apt - utils
ii acpi - support ii apache2 - bin ii apt - xapian - index
ii acpid ii apache2 - data ii aptdaemon
ii add - apt - key ii apache2 - utils ii aptdaemon - data
2017 - 01 - 06 13 : 19 Page 2
ii aptitude ii avahi - daemon ii bind9 - host
ii aptitude - common ii avahi - utils ii binfmt - support
ii apturl ii aview ii binutils
ii apturl - common ii banshee ii bison
ii archdetect - deb ii baobab ii blt
ii aspell ii base - files ii blueberry
ii aspell - en ii base - passwd ii bluetooth
ii at - spi2 - core ii bash ii bluez
ii attr ii bash - completion ii bluez - cups
ii avahi - autoipd ii bc ii bluez - obexd
.....
其中,使用的標(biāo)志如下:
10、 tr 命令行
這個(gè)命令從標(biāo)準(zhǔn)輸入轉(zhuǎn)換或者刪除字符,然后輸出結(jié)果到標(biāo)準(zhǔn)輸出。
使用 tr 的語(yǔ)法如下:
看一下下面的例子,在第一個(gè)命令,set1( [:upper:] ) 代表指定輸入字符的大小寫(都是大寫字符)。 set2([:lower:]) 代表期望結(jié)果字符的大小寫。第二個(gè)例子意思相似,轉(zhuǎn)義字符 \n 表示在新的一行打印輸出:
tecmint@TecMint ~ $ echo 'WWW.TECMINT.COM' | tr [: upper :] [: lower :]
www . tecmint . com
tecmint@TecMint ~ $ echo 'news.' | tr [: lower :] [: upper :]
NEWS . TECMINT . COM
11、 more 命令
more [17] 命令是一個(gè)有用的文件過濾器,最初為查看證書而建。它一頁(yè)頁(yè)顯示文件內(nèi)容,用戶可以通過按回車來顯示更多的信息。
你可以像這樣使用它來顯示大文件:
tecmint@TecMint ~ $ dmesg | more
[ 0.000000 ] Initializing cgroup subsys cpuset
[ 0.000000 ] Initializing cgroup subsys cpu
[ 0.000000 ] Initializing cgroup subsys cpuacct
[ 0.000000 ] Linux version 4.4 . 0 - 21 - generic ( buildd@lgw01 - 21 ) ( gcc version 5.3 . 1 20160413 ( Ubuntu 5.3 . 1 - 14ubuntu2 ) ) # 37 - Ubuntu SMP Mon Apr 18 18 : 33 : 37 UTC 2016 ( Ubuntu 4.4 . 0 - 21.37 - generic
4.4 . 6 )
[ 0.000000 ] Command line : BOOT_IMAGE = /boot/ vmlinuz - 4.4 . 0 - 21 - generic root = UUID = bb29dda3 - bdaa - 4b39 - 86cf - 4a6dc9634a1b ro quiet splash vt . handoff = 7
[ 0.000000 ] KERNEL supported cpus :
[ 0.000000 ] Intel GenuineIntel
[ 0.000000 ] AMD AuthenticAMD
[ 0.000000 ] Centaur CentaurHauls
[ 0.000000 ] x86 / fpu : xstate_offset [ 2 ]: 576 , xstate_sizes [ 2 ]: 256
[ 0.000000 ] x86 / fpu : Supporting XSAVE feature 0x01 : 'x87 floating point registers'
[ 0.000000 ] x86 / fpu : Supporting XSAVE feature 0x02 : 'SSE registers'
[ 0.000000 ] x86 / fpu : Supporting XSAVE feature 0x04 : 'AVX registers'
[ 0.000000 ] x86 / fpu : Enabled xstate features 0x7 , context size is 832 bytes , using 'standard' format .
[ 0.000000 ] x86 / fpu : Using 'eager' FPU context switches .
[ 0.000000 ] e820 : BIOS - provided physical RAM map :
[ 0.000000 ] BIOS - e820 : [ mem 0x0000000000000000 - 0x000000000009d3ff ] usable
[ 0.000000 ] BIOS - e820 : [ mem 0x000000000009d400 - 0x000000000009ffff ] reserved
[ 0.000000 ] BIOS - e820 : [ mem 0x00000000000e0000 - 0x00000000000fffff ] reserved
[ 0.000000 ] BIOS - e820 : [ mem 0x0000000000100000 - 0x00000000a56affff ] usable
[ 0.000000 ] BIOS - e820 : [ mem 0x00000000a56b0000 - 0x00000000a5eaffff ] reserved
[ 0.000000 ] BIOS - e820 : [ mem 0x00000000a5eb0000 - 0x00000000aaabefff ] usable
-- More --
12、 less 命令
less 是和上面的 more 命令相反的一個(gè)命令,但是它提供了額外的特性,而且對(duì)于大文件,它會(huì)更快些。
按照 more 命令相同的方式使用它:
tecmint@TecMint ~ $ dmesg | less
[ 0.000000 ] Initializing cgroup subsys cpuset
[ 0.000000 ] Initializing cgroup subsys cpu
[ 0.000000 ] Initializing cgroup subsys cpuacct
[ 0.000000 ] Linux version 4.4 . 0 - 21 - generic ( buildd@lgw01 - 21 ) ( gcc version 5.3 . 1 20160413 ( Ubuntu 5.3 . 1 - 14ubuntu2 ) ) # 37 - Ubuntu SMP Mon Apr 18 18 : 33 : 37 UTC 2016 ( Ubuntu 4.4 . 0 - 21.37 - generic
4.4 . 6 )
[ 0.000000 ] Command line : BOOT_IMAGE = /boot/ vmlinuz - 4.4 . 0 - 21 - generic root = UUID = bb29dda3 - bdaa - 4b39 - 86cf - 4a6dc9634a1b ro quiet splash vt . handoff = 7
[ 0.000000 ] KERNEL supported cpus :
[ 0.000000 ] Intel GenuineIntel
[ 0.000000 ] AMD AuthenticAMD
[ 0.000000 ] Centaur CentaurHauls
[ 0.000000 ] x86 / fpu : xstate_offset [ 2 ]: 576 , xstate_sizes [ 2 ]: 256
[ 0.000000 ] x86 / fpu : Supporting XSAVE feature 0x01 : 'x87 floating point registers'
[ 0.000000 ] x86 / fpu : Supporting XSAVE feature 0x02 : 'SSE registers'
[ 0.000000 ] x86 / fpu : Supporting XSAVE feature 0x04 : 'AVX registers'
[ 0.000000 ] x86 / fpu : Enabled xstate features 0x7 , context size is 832 bytes , using 'standard' format .
[ 0.000000 ] x86 / fpu : Using 'eager' FPU context switches .
[ 0.000000 ] e820 : BIOS - provided physical RAM map :
[ 0.000000 ] BIOS - e820 : [ mem 0x0000000000000000 - 0x000000000009d3ff ] usable
[ 0.000000 ] BIOS - e820 : [ mem 0x000000000009d400 - 0x000000000009ffff ] reserved
[ 0.000000 ] BIOS - e820 : [ mem 0x00000000000e0000 - 0x00000000000fffff ] reserved
[ 0.000000 ] BIOS - e820 : [ mem 0x0000000000100000 - 0x00000000a56affff ] usable
[ 0.000000 ] BIOS - e820 : [ mem 0x00000000a56b0000 - 0x00000000a5eaffff ] reserved
[ 0.000000 ] BIOS - e820 : [ mem 0x00000000a5eb0000 - 0x00000000aaabefff ] usable
:
學(xué)習(xí)為什么 Linux 下進(jìn)行有效的文件瀏覽, ‘less’ 比 ‘more’ 命令更快 [18] 。
基本上就這些了,如果你還知道其他本文沒有提供的 Linux 下有用的文本過濾命令行工具 [19] ,可以在下面的評(píng)論部分通知我們。
作者簡(jiǎn)介:Aaron Kili 是一名 Linux 和 F.O.S.S 愛好者、一名未來的 Linux 系統(tǒng)管理員、web 開發(fā)者,并且目前是一名 TecMint 上的內(nèi)容創(chuàng)造者,他喜歡計(jì)算機(jī)相關(guān)的工作,并且堅(jiān)信知識(shí)的分享。
via: http://www./linux-file-operations-commands/
作者:Aaron Kili [20] 譯者:yangmingming 校對(duì):jasminepeng
本文由 LCTT [21] 原創(chuàng)編譯,Linux中國(guó) 榮譽(yù)推出
[1]: awk - https:///tag-awk.html
[2]: awk 系列 1 到 13 部分 - https:///article-7586-1.html
[3]: sed - https:///tag-sed.html
[4]: 如何使用 GNU sed 命令在 Linux 下創(chuàng)建、編輯和處理文件 - https:///article-7161-1.html
[5]: 日常 Linux 系統(tǒng)管理員任務(wù)使用的 15 個(gè)有用的 sed 命令小貼士和技巧 - http://www./linux-sed-command-tips-tricks/
[6]: grep - https:///tag-grep.html
[7]: 使用特定的選項(xiàng)的 grep - http://www./linux-grep-commands-character-classes-bracket-expressions/
[8]: Linux 下的 grep、 egrep 和 fgrep 的差異? - http://www./difference-between-grep-egrep-and-fgrep-in-linux/
[9]: 使用帶有 tail 和 cat 命令的 head 命令 - http://www./view-contents-of-file-in-linux/
[10]: 實(shí)時(shí)查看一個(gè)文件的變化 - http://www./fswatch-monitors-files-and-directory-changes-modifications-in-linux/
[11]: sort - https:///tag-sort.html
[12]: sort 命令 - https:///tag-sort.html
[13]: Linux 的 ‘sort’命令的14個(gè)有用的范例(一) - https:///article-5372-1.html
[14]: Linux 的 'sort'命令的七個(gè)有趣實(shí)例(二) - https:///article-5373-1.html
[15]: 如何基于修改日期和時(shí)間來查找和排序文件 - http://www./find-and-sort-files-modification-date-and-time-in-linux/
[16]: uniq - https:///tag-uniq.html
[17]: more - https:///tag-more.html
[18]: ‘less’ 比 ‘more’ 命令更快 - http://www./linux-more-command-and-less-command-examples/
[19]: 有用的文本過濾命令行工具 - http://www./tag/linux-tricks/
[20]: Aaron Kili - http://www./author/aaronkili/
[21]: LCTT - https://github.com/LCTT/TranslateProject