网站搜索

在 Linux 中过滤文本以实现有效文件操作的 12 个有用命令


在本文中,我们将回顾一些在 Linux 中充当过滤器的命令行工具。 过滤器是一个读取标准输入、对其执行操作并将结果写入标准输出的程序。

因此,它可以用于以强大的方式处理信息,例如重组输出以生成有用的报告、修改文件中的文本以及许多其他系统管理任务。

话虽如此,下面是 Linux 中一些有用的文件或文本过滤器。

1.awk命令

Awk 是一种出色的模式扫描和处理语言,它可用于在 Linux 中构建有用的过滤器。您可以通过阅读我们的 Awk 系列第 1 部分到第 13 部分来开始使用它。

此外,还可以阅读 awk 手册页以获取更多信息和使用选项:

man awk

2.sed命令

sed 是一个功能强大的流编辑器,用于过滤和转换文本。我们已经写了两篇关于 sed 的有用文章,您可以在这里阅读:

  1. 如何使用 GNU ‘sed’ 命令在 Linux 中创建、编辑和操作文件
  2. 用于日常 Linux 系统管理任务的 15 个有用的“sed”命令提示和技巧

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 有何区别? 的更多信息。

4.头命令

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

了解如何将 head 命令与 tail 和 cat 命令结合使用,以便在 Linux 中有效使用。

5. 尾部命令

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 用于对文本文件或标准输入中的行进行排序。

以下是名为 domains.list 的文件的内容:

tecmint@TecMint ~ $ cat domains.list
linux-console.net
linux-console.net
news.linux-console.net
news.linux-console.net
linuxsay.com
linuxsay.com
windowsmint.com
windowsmint.com

您可以运行一个简单的排序命令来对文件内容进行排序,如下所示:

tecmint@TecMint ~ $ sort domains.list
linuxsay.com
linuxsay.com
news.linux-console.net
news.linux-console.net
linux-console.net
linux-console.net
windowsmint.com
windowsmint.com

您可以通过多种方式使用 sort 命令,浏览一些有关 sort 命令的有用文章,如下所示:

  1. Linux ‘sort’ 命令的 14 个有用示例 – 第 1 部分
  2. 7 个有趣的 Linux“sort”命令示例 – 第 2 部分
  3. 如何根据修改日期和时间查找文件并对其进行排序
  4. 如何按上次修改日期和时间对“ls”命令的输出进行排序

7.uniq命令

uniq 命令用于报告或省略重复的行,它过滤标准输入中的行并将结果写入标准输出。

在输入流上运行sort后,您可以使用uniq删除重复的行,如下例所示。

要指示行出现的次数,请使用 -c 选项,并在通过包含 -i 选项进行比较时忽略大小写差异:

tecmint@TecMint ~ $ cat domains.list
linux-console.net
linux-console.net
news.linux-console.net
news.linux-console.net
linuxsay.com
linuxsay.com
windowsmint.com

tecmint@TecMint ~ $ sort domains.list | uniq -c 
2 linuxsay.com
2 news.linux-console.net
2 linux-console.net
1 windowsmint.com 

通读 uniq 手册页以获取更多使用信息和标志:

man uniq

8.fmt命令

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

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

1.linux-console.net 2.news.linux-console.net 3.linuxsay.com 4.windowsmint.com

要将上述内容重新格式化为标准列表,请运行以下命令,并使用 -w 开关来定义最大行宽:

tecmint@TecMint ~ $ cat domain-list.txt 
1.linux-console.net 2.news.linux-console.net 3.linuxsay.com 4.windowsmint.com

tecmint@TecMint ~ $ fmt -w 1 domain-list.txt
1.linux-console.net 
2.news.linux-console.net 
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

.....

这里使用的标志是:

  1. --column 定义在输出中创建的列数。
  2. -l 指定页面长度(默认为 66 行)。

10.tr命令

该工具翻译或删除标准输入中的字符并将结果写入标准输出。

使用tr的语法如下:

tr options set1 set2

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

然后 set2([:lower:]) 表示结果字符的情况。第二个示例中的情况相同,转义序列 \n 表示在新行上打印输出:

tecmint@TecMint ~ $ echo "WWW.TECMINT.COM" | tr [:upper:] [:lower:]
linux-console.net

tecmint@TecMint ~ $ echo "news.linux-console.net" | tr [:lower:] [:upper:]
NEWS.TECMINT.COM

11.更多命令

more 命令是一个有用的文件细读过滤器,主要用于查看证书。它以类似页面的格式显示文件内容,用户可以按[Enter]查看更多信息。

您可以使用它来查看大文件,如下所示:

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”命令在 Linux 中进行有效文件导航更快。

现在就这些了,请通过下面的评论部分让我们了解此处未提及的任何有用的命令行工具,这些工具在 Linux 中充当文本过滤器。