在本文中,我们将回顾一些在 Linux 中充当过滤器的命令行工具。一个筛选是一个读取标准输入、对其执行操作并将结果写入标准输出的程序。
因此,它可以用于以强大的方式处理信息,例如重组输出以生成有用的报告、修改文件中的文本以及许多其他系统管理任务。
话虽如此,下面是 Linux 中一些有用的文件或文本过滤器。
1.awk命令
awk是一种出色的模式扫描和处理语言,它可用于在 Linux 中构建有用的过滤器。您可以通过阅读我们的内容开始使用它。
此外,还请阅读awk有关更多信息和使用选项的手册页:
$ man awk
2.sed命令
sed是一个强大的流编辑器,用于过滤和转换文本。我们已经写了两篇关于 sed 的有用文章,您可以在这里阅读:
sed 手册页添加了控制选项和指令:
$ man sed
3.Grep、Egrep、Fgrep、Rgrep 命令
这些过滤器输出与给定模式匹配的行。它们从文件或标准输入读取行,并默认将所有匹配的行打印到标准输出。
笔记: 主要程序是,变化与如下(并且它们仍然被用于向后兼容):
$ 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:
您可以阅读更多有关。
4.头命令
头用于显示文件的第一部分,它输出第一个10默认情况下的行数。您可以使用-nnum 标志指定要显示的行数:
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
了解如何使用在 Linux 中有效使用。
5. 尾部命令
尾巴输出最后部分(10文件的默认行数)。使用-nnum 开关指定要显示的行数。
下面的命令将输出最后一个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.
此外,尾巴有一个特殊的选项-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.
通读尾巴使用选项和说明的完整列表的手册页:
$ man tail
6.排序命令
种类用于对文本文件或标准输入中的行进行排序。
以下是名为的文件的内容域名列表:
tecmint@TecMint ~ $ cat domains.list tecmint.com tecmint.com news.tecmint.com news.tecmint.com linuxsay.com linuxsay.com windowsmint.com windowsmint.com
你可以运行一个简单的像这样对文件内容进行排序:
tecmint@TecMint ~ $ sort domains.list linuxsay.com linuxsay.com news.tecmint.com news.tecmint.com tecmint.com tecmint.com windowsmint.com windowsmint.com
你可以使用种类命令的方式有很多种,请浏览一些有关 sort 命令的有用文章,如下所示:
7.uniq命令
独特的命令用于报告或省略重复的行,它从标准输入中过滤行并将结果写入标准输出。
运行后种类在输入流上,您可以使用以下命令删除重复的行独特的如下例所示。
要指示某行出现的次数,请使用-c选项并在比较时忽略大小写差异,包括-i选项:
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
通读独特的有关更多使用信息和标志的手册页:
$ man uniq
8.fmt命令
FMMT简单的最佳文本格式化程序,它重新格式化指定文件中的段落并将结果打印到标准输出。
以下是从文件中提取的内容域列表.txt:
1.tecmint.com 2.news.tecmint.com 3.linuxsay.com 4.windowsmint.com
要将上述内容重新格式化为标准列表,请运行以下命令-wswitch 用于定义最大线宽:
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命令
公关命令转换文本文件或标准输入以进行打印。例如在德班系统中,您可以列出所有已安装的软件包,如下所示:
$ 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命令
该工具翻译或删除标准输入中的字符并将结果写入标准输出。
使用的语法t如下:
$ tr options set1 set2
看一下下面的示例,在第一个命令中,set1( [:upper:] )表示输入字符的大小写(全部大写)。
然后set2([:lower:])表示结果字符的情况。第二个示例和转义序列中的情况相同\n意味着在新行上打印输出:
tecmint@TecMint ~ $ echo "cdgzu.com" | tr [:upper:] [:lower:] cdgzu.com tecmint@TecMint ~ $ echo "news.tecmint.com" | tr [:lower:] [:upper:] NEWS.TECMINT.COM
11.更多命令
更多的命令是一个有用的文件细读过滤器,主要用于查看证书。它以类似页面的格式显示文件内容,用户可以在其中按[进入]查看更多信息。
您可以使用它来查看大文件,如下所示:
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 命令
较少的是相反的更多的上面的命令,但它提供了额外的功能,并且处理大文件的速度要快一些。
使用方式与 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 :
了解原因在 Linux 中进行有效的文件导航。
目前就这些,如有任何问题请告诉我们这里没有提到,它通过下面的评论部分充当 Linux 中的文本过滤器。
