过滤器工具以有效的方式处理信息,比如重构输出以生成实用报告,修改文件中的文本,以及处理其他许多系统管理任务。

Linux下过滤文本、实现高效文件操作的12个实用命令

言归正传,下面介绍几款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下如何实现高效的文件导航。