如何使用Awk和正则表达式过滤文本或字符串-第1部分

当我们在Linux中运行某些命令来读取或编辑字符串或文件中的文本时,我们经常会尝试过滤输出以获得感兴趣的特定部分。这就是使用正则表达式的地方。

什么是正则表达式?

A regular expression can be defined as strings that represent several sequences of characters. One of the most important things about regular expressions is that they allow you to filter the output of a command or file, edit a section of a text or configuration file, and so on.

正则表达式的特点

正则表达式由以下组成:

  • 普通字符,如空格,下划线(_),A-Z,a-z,0-9。
  • 元字符,它们被扩展为普通字符,包括:
    • (.) 它匹配除换行符之外的任何单个字符。
    • (*) 它匹配其前面的字符的零个或多个存在。
    • [字符(们)] 它匹配字符(们)中指定的任何一个字符,也可以使用连字符(-)表示字符范围,如[a-f][1-5]等。
    • ^ 它匹配文件中一行的开头。
    • $ 匹配文件中一行的结尾。
    • \ 它是一个转义字符。

为了过滤文本,必须使用诸如 awk 的文本过滤工具。您可以将 awk 视为一种独立的编程语言。但在本指南中,我们将其作为一个简单的 命令行过滤工具 来介绍 awk 的使用方法。

awk 的一般语法为:

awk 'script' filename

其中 'script' 是一组由 awk 理解并在文件 filename 上执行的命令。

它通过读取文件中的给定行,复制该行,然后在该行上执行脚本来工作。这个过程在文件的所有行上都重复进行。

'script' 的形式为 '/pattern/ action',其中 pattern 是一个正则表达式,而 action 是当 awk 在行中找到给定模式时执行的操作。

如何在 Linux 中使用 Awk 过滤工具

在接下来的示例中,我们将重点关注上面讨论的 awk 特性下的元字符。

使用 Awk 打印文件中的所有行

以下示例打印文件 /etc/hosts 中的所有行,因为未提供模式。

awk '//{print}'/etc/hosts
Awk Prints All Lines in a File

使用 Awk 模式:在文件中匹配含有“localhost”的行

在下面的示例中,模式localhost已经给出,因此awk将匹配在/etc/hosts文件中具有localhost的行。

awk '/localhost/{print}' /etc/hosts 
Awk Print Given Matching Line in a File

在模式中使用Awk与(.)通配符

(.)将匹配包含loclocalhostlocalnet的字符串,如下面的示例所示。

也就是说* l某个单字符c *

awk '/l.c/{print}' /etc/hosts
Use Awk to Print Matching Strings in a File

在模式中使用Awk与(*)字符

它将匹配包含localhostlocalnetlinescapable的字符串,如下面的示例所示:

awk '/l*c/{print}' /etc/localhost
Use Awk to Match Strings in File

您还会意识到(*)尝试获取可能的最长匹配。

让我们看一个演示这一点的案例,以正则表达式t*t为例,它表示匹配以下行以字母t开头并以t结尾的字符串:

this is tecmint, where you get the best good tutorials, how to's, guides, tecmint. 

当您使用模式/t*t/时,您将得到以下可能性:

this is t
this is tecmint
this is tecmint, where you get t
this is tecmint, where you get the best good t
this is tecmint, where you get the best good tutorials, how t
this is tecmint, where you get the best good tutorials, how tos, guides, t
this is tecmint, where you get the best good tutorials, how tos, guides, tecmint

而在/t*t/中的(*)通配符字符允许awk选择最后一个选项:

this is tecmint, where you get the best good tutorials, how to's, guides, tecmint

在设置[字符]中使用Awk

[al1]为例,这里awk将匹配包含字符al1的所有字符串/etc/hosts中的行。

awk '/[al1]/{print}' /etc/hosts
Use-Awk to Print Matching Character in File

下一个示例匹配以Kk开头,后跟T的字符串:

# awk '/[Kk]T/{print}' /etc/hosts 
Use Awk to Print Matched String in File

指定范围内的字符

了解awk中的字符:

  • [0-9]表示单个数字
  • [a-z]表示匹配单个小写字母
  • [A-Z]表示匹配单个大写字母
  • [a-zA-Z]表示匹配单个字母
  • [a-zA-Z 0-9]表示匹配单个字母或数字

让我们看一个例子:

awk '/[0-9]/{print}' /etc/hosts 
Use Awk To Print Matching Numbers in File

文件/etc/hosts中的所有行在上述例子中至少包含一个数字[0-9]

使用带有(^)元字符的Awk

它匹配所有以所提供的模式开头的行,如下例所示:

# awk '/^fe/{print}' /etc/hosts
# awk '/^ff/{print}' /etc/hosts
Use Awk to Print All Matching Lines with Pattern

使用带有($)元字符的Awk

它匹配所有以提供的模式结尾的行:

awk '/ab$/{print}' /etc/hosts
awk '/ost$/{print}' /etc/hosts
awk '/rs$/{print}' /etc/hosts
Use Awk to Print Given Pattern String

使用带有(\)转义字符的Awk

它允许您将其后的字符视为文字,即将其视为原样。

在下面的例子中,第一条命令打印出文件中的所有行,第二条命令不打印任何内容,因为我想匹配一个具有$25.00的行,但未使用转义字符。

第三个命令是正确的,因为已使用转义字符将$视为原样。

awk '//{print}' deals.txt
awk '/$25.00/{print}' deals.txt
awk '/\$25.00/{print}' deals.txt
Use Awk with Escape Character
总结

这还不是所有的awk命令行过滤工具,上述示例是awk的基本操作。在接下来的部分中,我们将介绍如何使用awk的复杂特性。

感谢阅读并为任何添加或澄清事项,请在评论部分发表评论。

Source:
https://www.tecmint.com/use-linux-awk-command-to-filter-text-string-in-files/