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

译文
系统 Linux
我们在本文中介绍了多款在Linux下充当过滤器的命令行工具。过滤器是这样一种程序:读取标准输入后,对它执行操作,然后将结果写入到标准输出。过滤器工具以有效的方式处理信息,比如重构输出以生成实用报告,修改文件中的文本,以及处理其他许多系统管理任务。

【51CTO.com快译】我们在本文中介绍了多款在Linux下充当过滤器的命令行工具。过滤器是这样一种程序:读取标准输入后,对它执行操作,然后将结果写入到标准输出。

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

Linux

言归正传,下面介绍几款Linux环境下实用的文件或文本过滤器。

1.Awk命令

Awk是一种出色的模式扫描和处理语言,它可以用来在Linux下构建实用过滤器。如果你从头至尾看过我们编写的Awk系列文章:第1部分至第13部分(http://www.tecmint.com/category/awk-command/),就可以开始使用它。

另外,还可以参阅awk的参考手册页,了解更多信息和用法选项:

  1. $ 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的参考手册页添加了控制选项和操作说明:

  1. $ man sed 

3.Grep、Egrep、Fgrep和Rgrep命令

这些过滤器输出与特定模式匹配的行。它们从文件或标准输入读取行,默认情况下将所有匹配的行打印输出到标准输出。

注意:主程序是grep,几个变种与使用特定的grep选项完全一样(它们仍可用于向后兼容): 

  1. $ egrep = grep -E 
  2. $ fgrep = grep -F 
  3. $ rgrep = grep -r  

下面是一些基本的grep命令:

  1. tecmint@TecMint ~ $ grep "aaronkilik" /etc/passwd 
  2. aaronkilik:x:1001:1001::/home/aaronkilik: 
  3. tecmint@TecMint ~ $ cat /etc/passwd | grep "aronkilik" 
  4. 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标志,指定显示的行数:

  1. tecmint@TecMint ~ $ head /var/log/auth.log   
  2. Jan  2 10:45:01 TecMint CRON[3383]: pam_unix(cron:session): session opened for user root by (uid=0) 
  3. Jan  2 10:45:01 TecMint CRON[3383]: pam_unix(cron:session): session closed for user root 
  4. Jan  2 10:51:34 TecMint sudo:  tecmint : TTY=unknown ; PWD=/home/tecmint ; USER=root ; COMMAND=/usr/lib/linuxmint/mintUpdate/checkAPT.py 
  5. Jan  2 10:51:34 TecMint sudo: pam_unix(sudo:session): session opened for user root by (uid=0) 
  6. Jan  2 10:51:39 TecMint sudo: pam_unix(sudo:session): session closed for user root 
  7. Jan  2 10:55:01 TecMint CRON[4099]: pam_unix(cron:session): session opened for user root by (uid=0) 
  8. Jan  2 10:55:01 TecMint CRON[4099]: pam_unix(cron:session): session closed for user root 
  9. Jan  2 11:05:01 TecMint CRON[4138]: pam_unix(cron:session): session opened for user root by (uid=0) 
  10. Jan  2 11:05:01 TecMint CRON[4138]: pam_unix(cron:session): session closed for user root 
  11. Jan  2 11:09:01 TecMint CRON[4146]: pam_unix(cron:session): session opened for user root by (uid=0) 
  12. tecmint@TecMint ~ $ head  -n 5 /var/log/auth.log   
  13. Jan  2 10:45:01 TecMint CRON[3383]: pam_unix(cron:session): session opened for user root by (uid=0) 
  14. Jan  2 10:45:01 TecMint CRON[3383]: pam_unix(cron:session): session closed for user root 
  15. Jan  2 10:51:34 TecMint sudo:  tecmint : TTY=unknown ; PWD=/home/tecmint ; USER=root ; COMMAND=/usr/lib/linuxmint/mintUpdate/checkAPT.py 
  16. Jan  2 10:51:34 TecMint sudo: pam_unix(sudo:session): session opened for user root by (uid=0) 
  17. Jan  2 10:51:39 TecMint sudo: pam_unix(sudo:session): session closed for user root  

5.tail命令

tail输出文件的末尾部分(默认情况下是末尾10行)。使用-n num参数选项符,即可指定显示的行数。

下面这个命令会输出指定文件的末尾5行:

  1. tecmint@TecMint ~ $ tail -n 5 /var/log/auth.log 
  2. Jan  6 13:01:27 TecMint sshd[1269]: Server listening on 0.0.0.0 port 22. 
  3. Jan  6 13:01:27 TecMint sshd[1269]: Server listening on :: port 22. 
  4. Jan  6 13:01:27 TecMint sshd[1269]: Received SIGHUP; restarting. 
  5. Jan  6 13:01:27 TecMint sshd[1269]: Server listening on 0.0.0.0 port 22. 
  6. Jan  6 13:01:27 TecMint sshd[1269]: Server listening on :: port 22.  

此外,tail有一个特殊的选项-f,可用于实时查看文件(尤其是日志文件)的变化。

下面这个命令让你能够密切关注指定文件的变化:

  1. tecmint@TecMint ~ $ tail -f /var/log/auth.log 
  2. Jan  6 12:58:01 TecMint sshd[1269]: Server listening on :: port 22. 
  3. Jan  6 12:58:11 TecMint sshd[1269]: Received SIGHUP; restarting. 
  4. Jan  6 12:58:12 TecMint sshd[1269]: Server listening on 0.0.0.0 port 22. 
  5. Jan  6 12:58:12 TecMint sshd[1269]: Server listening on :: port 22. 
  6. Jan  6 13:01:27 TecMint sshd[1269]: Received SIGHUP; restarting. 
  7. Jan  6 13:01:27 TecMint sshd[1269]: Server listening on 0.0.0.0 port 22. 
  8. Jan  6 13:01:27 TecMint sshd[1269]: Server listening on :: port 22. 
  9. Jan  6 13:01:27 TecMint sshd[1269]: Received SIGHUP; restarting. 
  10. Jan  6 13:01:27 TecMint sshd[1269]: Server listening on 0.0.0.0 port 22. 
  11. Jan  6 13:01:27 TecMint sshd[1269]: Server listening on :: port 22.  

参阅tail的参考手册页,即可了解完整的用法选项和操作说明:

  1. $ man tail 

6.sort命令

sort用于排序文本文件的行或来自标准输入的行。

下面是一个名为domains.list的文件的内容:

  1. tecmint@TecMint ~ $ cat domains.list 
  2. tecmint.com 
  3. tecmint.com 
  4. news.tecmint.com 
  5. news.tecmint.com 
  6. linuxsay.com 
  7. linuxsay.com 
  8. windowsmint.com 
  9. windowsmint.com  

你可以运行简单的sort命令,排序文件内容,就像这样:

  1. tecmint@TecMint ~ $ sort domains.list 
  2. linuxsay.com 
  3. linuxsay.com 
  4. news.tecmint.com 
  5. news.tecmint.com 
  6. tecmint.com 
  7. tecmint.com 
  8. windowsmint.com 
  9. 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选项来比较:

  1. tecmint@TecMint ~ $ cat domains.list 
  2. tecmint.com 
  3. tecmint.com 
  4. news.tecmint.com 
  5. news.tecmint.com 
  6. linuxsay.com 
  7. linuxsay.com 
  8. windowsmint.com 
  9. sort domains.list | uniq -c  
  10. 2 linuxsay.com 
  11. 2 news.tecmint.com 
  12. 2 tecmint.com 
  13. 1 windowsmint.com   

阅读uniq的参考手册页,可进一步了解用法信息和标志:

  1. $ man uniq 

8.fmt命令

fmt是简单的最佳文本格式器,它可以重新格式化指定文件中的段落,并将结果打印输出到标准输出。

下面是从文件domain-list.txt提取的内容:

1.tecmint.com 2.news.tecmint.com 3.linuxsay.com 4.windowsmint.com

要将上述内容重新格式化成标准列表,运行下面这个命令,-w参数选项符用来定义最大行宽:

  1. tecmint@TecMint ~ $ cat domain-list.txt  
  2. 1.tecmint.com 2.news.tecmint.com 3.linuxsay.com 4.windowsmint.com 
  3. tecmint@TecMint ~ $ fmt -w 1 domain-list.txt 
  4. 1.tecmint.com  
  5. 2.news.tecmint.com  
  6. 3.linuxsay.com  
  7. 4.windowsmint.com  

9.pr命令

pr命令可转换文本文件或标准输入,以便打印输出。比如在Debian系统上,你可以列出所有已安装的程序包,如下所示:

  1. $ dpkg -l 

想组织整理分成页和列的列表、准备打印输出,运行下面这个命令。

  1. tecmint@TecMint ~ $ dpkg -l | pr --columns 3 -l 20   
  2. 2017-01-06 13:19                                
  3.           Page 1 
  4. Desired=Unknown/Install  ii  adduser            ii  apg 
  5. | Status=Not/Inst/Conf-   ii  adwaita-icon-theme    ii  app-install-data 
  6. |/ Err?=(none)/Reinst-r    ii  adwaita-icon-theme-  ii  apparmor 
  7. ||/ Name              ii  alsa-base         ii  apt 
  8. +++-=============== ii  alsa-utils              ii  apt-clone 
  9. ii  accountsservice       ii  anacron               ii  apt-transport-https 
  10. ii  acl               ii  apache2               ii  apt-utils 
  11. ii  acpi-support        ii  apache2-bin         ii  apt-xapian-index 
  12. ii  acpid                 ii  apache2-data          ii  aptdaemon 
  13. ii  add-apt-key       ii  apache2-utils         ii  aptdaemon-data 
  14. 2017-01-06 13:19                                   
  15.            Page 2 
  16. ii  aptitude              ii  avahi-daemon      ii  bind9-host 
  17. ii  aptitude-common   ii  avahi-utils           ii  binfmt-support 
  18. ii  apturl                ii  aview             ii  binutils 
  19. ii  apturl-common         ii  banshee               ii  bison 
  20. ii  archdetect-deb       ii  baobab             ii  blt 
  21. ii  aspell                ii  base-files            ii  blueberry 
  22. ii  aspell-en             ii  base-passwd           ii  bluetooth 
  23. ii  at-spi2-core          ii  bash              ii  bluez 
  24. ii  attr                  ii  bash-completion       ii  bluez-cups 
  25. ii  avahi-autoipd         ii  bc                    ii  bluez-obexd 
  26. .....  

这里使用的标志如下:

--column定义输出中创建的列数。

-l 指定页长(默认页长是66行)。

10.tr命令

这个工具可转换或删除来自标准输入的字符,并将结果写入到标准输出。

使用tr的语法如下:

  1. $ tr options set1 set2 

不妨看一看下面的例子,在第一个命令中,set1([:upper:])表示输入字符的大小写(全是大写)。

然后,set2([:lower:])表示随后得到的字符会是小写。第二个例子中一样,换码顺序\n意味着打印输出到新行上:

  1. tecmint@TecMint ~ $ echo "WWW.TECMINT.COM" | tr [:upper:] [:lower:] 
  2. www.tecmint.com 
  3. tecmint@TecMint ~ $ echo "news.tecmint.com" | tr [:lower:] [:upper:] 
  4. NEWS.TECMINT.COM  

11.more命令

more命令是一个实用的文件阅读过滤器,基本上是用于查看证书而创建的。它显示了页面格式的文件内容,用户可以按回车键来查看更多信息。

你可以用它查看更广庞大的文件,就像这样:

  1. tecmint@TecMint ~ $ dmesg | more 
  2. [    0.000000] Initializing cgroup subsys cpuset 
  3. [    0.000000] Initializing cgroup subsys cpu 
  4. [    0.000000] Initializing cgroup subsys cpuacct 
  5. [    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 
  6. 4.4.6) 
  7. [    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 
  8. [    0.000000] KERNEL supported cpus: 
  9. [    0.000000]   Intel GenuineIntel 
  10. [    0.000000]   AMD AuthenticAMD 
  11. [    0.000000]   Centaur CentaurHauls 
  12. [    0.000000] x86/fpu: xstate_offset[2]:  576, xstate_sizes[2]:  256 
  13. [    0.000000] x86/fpu: Supporting XSAVE feature 0x01: 'x87 floating point registers' 
  14. [    0.000000] x86/fpu: Supporting XSAVE feature 0x02: 'SSE registers' 
  15. [    0.000000] x86/fpu: Supporting XSAVE feature 0x04: 'AVX registers' 
  16. [    0.000000] x86/fpu: Enabled xstate features 0x7, context size is 832 bytes, using 'standard' format. 
  17. [    0.000000] x86/fpu: Using 'eager' FPU context switches. 
  18. [    0.000000] e820: BIOS-provided physical RAM map: 
  19. [    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009d3ff] usable 
  20. [    0.000000] BIOS-e820: [mem 0x000000000009d400-0x000000000009ffff] reserved 
  21. [    0.000000] BIOS-e820: [mem 0x00000000000e0000-0x00000000000fffff] reserved 
  22. [    0.000000] BIOS-e820: [mem 0x0000000000100000-0x00000000a56affff] usable 
  23. [    0.000000] BIOS-e820: [mem 0x00000000a56b0000-0x00000000a5eaffff] reserved 
  24. [    0.000000] BIOS-e820: [mem 0x00000000a5eb0000-0x00000000aaabefff] usable 
  25. --More-- 

 12.less命令

less的用途与上面的more命令恰好相反,不过它提供了额外的功能,处理大文件时要快一点。

可以与more同样的方式来使用它:

  1. tecmint@TecMint ~ $ dmesg | less 
  2. [    0.000000] Initializing cgroup subsys cpuset 
  3. [    0.000000] Initializing cgroup subsys cpu 
  4. [    0.000000] Initializing cgroup subsys cpuacct 
  5. [    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 
  6. 4.4.6) 
  7. [    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 
  8. [    0.000000] KERNEL supported cpus: 
  9. [    0.000000]   Intel GenuineIntel 
  10. [    0.000000]   AMD AuthenticAMD 
  11. [    0.000000]   Centaur CentaurHauls 
  12. [    0.000000] x86/fpu: xstate_offset[2]:  576, xstate_sizes[2]:  256 
  13. [    0.000000] x86/fpu: Supporting XSAVE feature 0x01: 'x87 floating point registers' 
  14. [    0.000000] x86/fpu: Supporting XSAVE feature 0x02: 'SSE registers' 
  15. [    0.000000] x86/fpu: Supporting XSAVE feature 0x04: 'AVX registers' 
  16. [    0.000000] x86/fpu: Enabled xstate features 0x7, context size is 832 bytes, using 'standard' format. 
  17. [    0.000000] x86/fpu: Using 'eager' FPU context switches. 
  18. [    0.000000] e820: BIOS-provided physical RAM map: 
  19. [    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009d3ff] usable 
  20. [    0.000000] BIOS-e820: [mem 0x000000000009d400-0x000000000009ffff] reserved 
  21. [    0.000000] BIOS-e820: [mem 0x00000000000e0000-0x00000000000fffff] reserved 
  22. [    0.000000] BIOS-e820: [mem 0x0000000000100000-0x00000000a56affff] usable 
  23. [    0.000000] BIOS-e820: [mem 0x00000000a56b0000-0x00000000a5eaffff] reserved 
  24. [    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】

责任编辑:庞桂玉 来源: 51CTO.com
相关推荐

2017-02-27 14:50:36

Linux命令数据

2023-11-06 18:02:28

Linux实用命令

2020-09-28 15:14:31

Linux常用命令实用命令

2015-10-29 13:10:08

passwd命令Linux

2015-07-27 09:22:53

Unix文件系统命令

2015-09-23 09:22:01

系统硬件命令

2020-10-29 18:42:26

Linux命令操作系统

2015-10-22 17:20:46

命令工具Linux

2014-08-07 10:15:27

linux

2016-12-07 18:22:23

shelllinuxgrep

2009-12-17 10:07:40

linuxpv管道查看器

2020-04-25 19:00:15

Linux终端命令

2010-02-24 14:50:33

Ubuntu vim

2013-10-08 15:51:03

Linux find命

2014-03-17 17:27:51

Linux mvLinux 命令

2013-05-23 15:10:34

Netcat

2023-10-30 10:47:45

Linux内核模块

2017-12-27 10:20:01

Linux ls命令实用范例

2017-12-27 09:40:32

Linuxfind命令

2019-07-31 08:55:01

LinuxUbuntu技术
点赞
收藏

51CTO技术栈公众号