Linux下過(guò)濾文本、實(shí)現(xiàn)高效文件操作的12個(gè)實(shí)用命令
譯文【51CTO.com快譯】我們?cè)诒疚闹薪榻B了多款在Linux下充當(dāng)過(guò)濾器的命令行工具。過(guò)濾器是這樣一種程序:讀取標(biāo)準(zhǔn)輸入后,對(duì)它執(zhí)行操作,然后將結(jié)果寫入到標(biāo)準(zhǔn)輸出。
過(guò)濾器工具以有效的方式處理信息,比如重構(gòu)輸出以生成實(shí)用報(bào)告,修改文件中的文本,以及處理其他許多系統(tǒng)管理任務(wù)。
言歸正傳,下面介紹幾款Linux環(huán)境下實(shí)用的文件或文本過(guò)濾器。
1.Awk命令
Awk是一種出色的模式掃描和處理語(yǔ)言,它可以用來(lái)在Linux下構(gòu)建實(shí)用過(guò)濾器。如果你從頭至尾看過(guò)我們編寫的Awk系列文章:第1部分至第13部分(http://www.tecmint.com/category/awk-command/),就可以開(kāi)始使用它。
另外,還可以參閱awk的參考手冊(cè)頁(yè),了解更多信息和用法選項(xiàng):
- $ man awk
2.Sed命令
sed是一種強(qiáng)大的流編輯器,可用于過(guò)濾和轉(zhuǎn)換文本。我們已經(jīng)編寫過(guò)介紹sed的兩篇實(shí)用文章,你可以在此閱讀:
《如何使用GNU “sed”命令在Linux下創(chuàng)建、編輯和處理文件》(http://www.tecmint.com/sed-command-to-create-edit-and-manipulate-files-in-linux/)
《處理日常Linux系統(tǒng)管理任務(wù)的15個(gè)實(shí)用的“sed”命令技巧和方法》(http://www.tecmint.com/linux-sed-command-tips-tricks/)
sed的參考手冊(cè)頁(yè)添加了控制選項(xiàng)和操作說(shuō)明:
- $ man sed
3.Grep、Egrep、Fgrep和Rgrep命令
這些過(guò)濾器輸出與特定模式匹配的行。它們從文件或標(biāo)準(zhǔn)輸入讀取行,默認(rèn)情況下將所有匹配的行打印輸出到標(biāo)準(zhǔn)輸出。
注意:主程序是grep,幾個(gè)變種與使用特定的grep選項(xiàng)完全一樣(它們?nèi)钥捎糜谙蚝蠹嫒?:
- $ 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:
詳細(xì)內(nèi)容請(qǐng)參閱《Linux下Grep、Egrep和Fgrep之間有何區(qū)別?》(http://www.tecmint.com/difference-between-grep-egrep-and-fgrep-in-linux/)。
4.head命令
head用于顯示文件的最初部分,默認(rèn)情況下輸出頭10行。你可以使用-n num標(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
5.tail命令
tail輸出文件的末尾部分(默認(rèn)情況下是末尾10行)。使用-n num參數(shù)選項(xiàng)符,即可指定顯示的行數(shù)。
下面這個(gè)命令會(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è)命令讓你能夠密切關(guā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的參考手冊(cè)頁(yè),即可了解完整的用法選項(xiàng)和操作說(shuō)明:
- $ man tail
6.sort命令
sort用于排序文本文件的行或來(lái)自標(biāo)準(zhǔn)輸入的行。
下面是一個(gè)名為domains.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)行簡(jiǎn)單的sort命令,排序文件內(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命令有好多方式,我們編寫了幾篇實(shí)用文章來(lái)介紹sort命令,如下所示:
《Linux “sort”命令的14個(gè)實(shí)用例子-第1部分》(http://www.tecmint.com/sort-command-linux/)
《7個(gè)有趣的Linux “sort”命令例子-第2部分》(http://www.tecmint.com/linux-sort-command-examples/)
《如何基于修改日期和時(shí)間來(lái)查找和排序文件》(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命令用于報(bào)告或忽略重復(fù)的行,它可以過(guò)濾來(lái)自標(biāo)準(zhǔn)輸入的行,并將結(jié)果寫入到標(biāo)準(zhǔn)輸出。
對(duì)輸入流運(yùn)行sort后,可以用uniq來(lái)消除重復(fù)的行,如下面這個(gè)例子所示。
為了表明某行出現(xiàn)的次數(shù),可使用-c選項(xiàng),忽視大小寫區(qū)別,同時(shí)通過(guò)加入-i選項(xiàng)來(lá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的參考手冊(cè)頁(yè),可進(jìn)一步了解用法信息和標(biāo)志:
- $ man uniq
8.fmt命令
fmt是簡(jiǎn)單的最佳文本格式器,它可以重新格式化指定文件中的段落,并將結(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)容重新格式化成標(biāo)準(zhǔn)列表,運(yùn)行下面這個(gè)命令,-w參數(shù)選項(xiàng)符用來(lái)定義最大行寬:
- 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)上,你可以列出所有已安裝的程序包,如下所示:
- $ dpkg -l
想組織整理分成頁(yè)和列的列表、準(zhǔn)備打印輸出,運(yùn)行下面這個(gè)命令。
- 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)志如下:
--column定義輸出中創(chuàng)建的列數(shù)。
-l 指定頁(yè)長(zhǎng)(默認(rèn)頁(yè)長(zhǎng)是66行)。
10.tr命令
這個(gè)工具可轉(zhuǎn)換或刪除來(lái)自標(biāo)準(zhǔn)輸入的字符,并將結(jié)果寫入到標(biāo)準(zhǔn)輸出。
使用tr的語(yǔ)法如下:
- $ tr options set1 set2
不妨看一看下面的例子,在第一個(gè)命令中,set1([:upper:])表示輸入字符的大小寫(全是大寫)。
然后,set2([:lower:])表示隨后得到的字符會(huì)是小寫。第二個(gè)例子中一樣,換碼順序\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命令是一個(gè)實(shí)用的文件閱讀過(guò)濾器,基本上是用于查看證書(shū)而創(chuàng)建的。它顯示了頁(yè)面格式的文件內(nèi)容,用戶可以按回車鍵來(lá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命令恰好相反,不過(guò)它提供了額外的功能,處理大文件時(shí)要快一點(diǎn)。
可以與more同樣的方式來(lái)使用它:
- 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下如何實(shí)現(xiàn)高效的文件導(dǎo)航。
要是還有哪些在Linux下可充當(dāng)文本過(guò)濾器的實(shí)用命令行工具是本文沒(méi)有提及的,歡迎留言補(bǔ)充。
原文標(biāo)題:12 Useful Commands For Filtering Text for Effective File Operations in Linux,作者:Aaron Kili
【51CTO譯稿,合作站點(diǎn)轉(zhuǎn)載請(qǐng)注明原文譯者和出處為51CTO.com】