Linux下過濾文本、實現高效文件操作的12個實用命令
譯文【51CTO.com快譯】我們在本文中介紹了多款在Linux下充當過濾器的命令行工具。過濾器是這樣一種程序:讀取標準輸入后,對它執行操作,然后將結果寫入到標準輸出。
過濾器工具以有效的方式處理信息,比如重構輸出以生成實用報告,修改文件中的文本,以及處理其他許多系統管理任務。
言歸正傳,下面介紹幾款Linux環境下實用的文件或文本過濾器。
1.Awk命令
Awk是一種出色的模式掃描和處理語言,它可以用來在Linux下構建實用過濾器。如果你從頭至尾看過我們編寫的Awk系列文章:第1部分至第13部分(http://www.tecmint.com/category/awk-command/),就可以開始使用它。
另外,還可以參閱awk的參考手冊頁,了解更多信息和用法選項:
- $ man awk
2.Sed命令
sed是一種強大的流編輯器,可用于過濾和轉換文本。我們已經編寫過介紹sed的兩篇實用文章,你可以在此閱讀:
《如何使用GNU “sed”命令在Linux下創建、編輯和處理文件》(http://www.tecmint.com/sed-command-to-create-edit-and-manipulate-files-in-linux/)
《處理日常Linux系統管理任務的15個實用的“sed”命令技巧和方法》(http://www.tecmint.com/linux-sed-command-tips-tricks/)
sed的參考手冊頁添加了控制選項和操作說明:
- $ man sed
3.Grep、Egrep、Fgrep和Rgrep命令
這些過濾器輸出與特定模式匹配的行。它們從文件或標準輸入讀取行,默認情況下將所有匹配的行打印輸出到標準輸出。
注意:主程序是grep,幾個變種與使用特定的grep選項完全一樣(它們仍可用于向后兼容):
- $ 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之間有何區別?》(http://www.tecmint.com/difference-between-grep-egrep-and-fgrep-in-linux/)。
4.head命令
head用于顯示文件的最初部分,默認情況下輸出頭10行。你可以使用-n num標志,指定顯示的行數:
- 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
5.tail命令
tail輸出文件的末尾部分(默認情況下是末尾10行)。使用-n num參數選項符,即可指定顯示的行數。
下面這個命令會輸出指定文件的末尾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有一個特殊的選項-f,可用于實時查看文件(尤其是日志文件)的變化。
下面這個命令讓你能夠密切關注指定文件的變化:
- 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 tail
6.sort命令
sort用于排序文本文件的行或來自標準輸入的行。
下面是一個名為domains.list的文件的內容:
- tecmint@TecMint ~ $ cat domains.list
- tecmint.com
- tecmint.com
- news.tecmint.com
- news.tecmint.com
- linuxsay.com
- linuxsay.com
- windowsmint.com
- windowsmint.com
你可以運行簡單的sort命令,排序文件內容,就像這樣:
- tecmint@TecMint ~ $ sort domains.list
- linuxsay.com
- linuxsay.com
- news.tecmint.com
- news.tecmint.com
- tecmint.com
- tecmint.com
- windowsmint.com
- windowsmint.com
使用sort命令有好多方式,我們編寫了幾篇實用文章來介紹sort命令,如下所示:
《Linux “sort”命令的14個實用例子-第1部分》(http://www.tecmint.com/sort-command-linux/)
《7個有趣的Linux “sort”命令例子-第2部分》(http://www.tecmint.com/linux-sort-command-examples/)
《如何基于修改日期和時間來查找和排序文件》(http://www.tecmint.com/find-and-sort-files-modification-date-and-time-in-linux/)
http://www.tecmint.com/sort-ls-output-by-last-modified-date-and-time/
7.uniq命令
uniq命令用于報告或忽略重復的行,它可以過濾來自標準輸入的行,并將結果寫入到標準輸出。
對輸入流運行sort后,可以用uniq來消除重復的行,如下面這個例子所示。
為了表明某行出現的次數,可使用-c選項,忽視大小寫區別,同時通過加入-i選項來比較:
- tecmint@TecMint ~ $ cat domains.list
- tecmint.com
- tecmint.com
- news.tecmint.com
- news.tecmint.com
- linuxsay.com
- linuxsay.com
- windowsmint.com
- sort domains.list | uniq -c
- 2 linuxsay.com
- 2 news.tecmint.com
- 2 tecmint.com
- 1 windowsmint.com
閱讀uniq的參考手冊頁,可進一步了解用法信息和標志:
- $ man uniq
8.fmt命令
fmt是簡單的最佳文本格式器,它可以重新格式化指定文件中的段落,并將結果打印輸出到標準輸出。
下面是從文件domain-list.txt提取的內容:
1.tecmint.com 2.news.tecmint.com 3.linuxsay.com 4.windowsmint.com
要將上述內容重新格式化成標準列表,運行下面這個命令,-w參數選項符用來定義最大行寬:
- 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命令可轉換文本文件或標準輸入,以便打印輸出。比如在Debian系統上,你可以列出所有已安裝的程序包,如下所示:
- $ dpkg -l
想組織整理分成頁和列的列表、準備打印輸出,運行下面這個命令。
- 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
- .....
這里使用的標志如下:
--column定義輸出中創建的列數。
-l 指定頁長(默認頁長是66行)。
10.tr命令
這個工具可轉換或刪除來自標準輸入的字符,并將結果寫入到標準輸出。
使用tr的語法如下:
- $ tr options set1 set2
不妨看一看下面的例子,在第一個命令中,set1([:upper:])表示輸入字符的大小寫(全是大寫)。
然后,set2([:lower:])表示隨后得到的字符會是小寫。第二個例子中一樣,換碼順序\n意味著打印輸出到新行上:
- tecmint@TecMint ~ $ echo "WWW.TECMINT.COM" | tr [:upper:] [:lower:]
- www.tecmint.com
- tecmint@TecMint ~ $ echo "news.tecmint.com" | tr [:lower:] [:upper:]
- NEWS.TECMINT.COM
11.more命令
more命令是一個實用的文件閱讀過濾器,基本上是用于查看證書而創建的。它顯示了頁面格式的文件內容,用戶可以按回車鍵來查看更多信息。
你可以用它查看更廣龐大的文件,就像這樣:
- 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命令恰好相反,不過它提供了額外的功能,處理大文件時要快一點。
可以與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
不妨參閱《為何“less”比“more”更快速?》(http://www.tecmint.com/linux-more-command-and-less-command-examples/)一文,即可了解在Linux下如何實現高效的文件導航。
要是還有哪些在Linux下可充當文本過濾器的實用命令行工具是本文沒有提及的,歡迎留言補充。
原文標題:12 Useful Commands For Filtering Text for Effective File Operations in Linux,作者:Aaron Kili
【51CTO譯稿,合作站點轉載請注明原文譯者和出處為51CTO.com】

























