当前位置:网站首页>awk从入门到入土(6)正则匹配
awk从入门到入土(6)正则匹配
2022-07-04 08:38:00 【奇妙之二进制】
awk的pattern除了使用简单的条件表达式之外,还可以用正则表达式。
AWK 可以方便高效地处理正则表达式。大量复杂的任务都可以由极其简单的正则表达式来解决。每一个精通命令行的人都知道正则表达式真正的威力所在。
这一章将着重讲解标准正则表达式的使用方法。awk的正则和普通正则基本一样,只不过在awk里需要将正则表达式用两个/包起来。
点(Dot)
点字符(.)可以匹配除了行结束字符的所有字符。比如下面的便子就可以匹配 fin, fun, fan 等等。
$ echo -e "cat\nbat\nfun\nfin\nfan" | awk '/f.n/'
执行上面命令可以得到如下结果:
fun
fin
fan
行开始
行开始符(^)匹配一行的开始。下面的示例将输出所有以字符串 The 开始的行。
$ echo -e "This\nThat\nThere\nTheir\nthese" | awk '/^The/'
执行上面的命令可以得到如下结果:
There
Their
行结束
行结束符($)匹配一行的结束。下面的例子中将输出所有以字符 n 结束的行:
$ echo -e "knife\nknow\nfun\nfin\nfan\nnine" | awk '/n$/'
执行上面的命令可以得到如下结果:
Ca
fin
fan
匹配字符集
匹配字符集用于匹配集合(由方括号表示)中的一个字符。如下例子中,匹配 Call 与 Tall 而不会匹配 Ball。
$ echo -e "Call\nTall\nBall" | awk '/[CT]all/'
执行上面的命令可以得到如下结果:
Call
Tall
排除集
正则匹配时会排除集合中的字符。如下例子中只会输出 Ball。
$ echo -e "Call\nTall\nBall" | awk '/[^CT]all/'
执行上面的命令可以得到如下结果:
Ball
或
竖线(|)允许正则表达式实现逻辑或运算. 下面例子将会输出 Ball 与 Call 。
$ echo -e "Call\nTall\nBall\nSmall\nShall" | awk '/Call|Ball/'
执行上面的命令可以得到如下结果:
Call
Ball
最多出现一次
该符号( ?)前面的字符不出现或者出现一次。如下示例匹配 Colour 与 Color。 使用 ? 使得 u 变成了可选字符 。
$ echo -e "Colour\nColor" | awk '/Colou?r/'
执行上面的命令可以得到如下结果:
Colour
Color
出现零次或多次
该符号(*) 允许其前的字符出现多次或者不出现。如下示例将匹配 ca,cat, catt 等等。
$ echo -e "ca\ncat\ncatt" | awk '/cat*/'
执行上面的命令可以得到如下结果:
ca
cat
catt
出现一次或多次
该符号(+)使得其前的字符出现一次或者多次。下面的例子会匹配一个 2 或者多个连续的 2。
$ echo -e "111\n22\n123\n234\n456\n222" | awk '/2+/'
执行上面的命令可以得到如下结果:
22
123
234
222
分组
括号用于分组而字符 | 用于提供多种选择。如下的正则表达式会匹配所有包含 Apple Juice 或 Aplle Cake 的行。
$ echo -e "Apple Juice\nApple Pie\nApple Tart\nApple Cake" | awk '/Apple (Juice|Cake)/'
执行上面的命令可以得到如下结果:
Apple Juice
Apple Cake
多个表达式可以使用&&或者,连接在一起,表示与的意思:
awk ‘/^开始时间日期/,/^结束时间日期/’ nginx.log
比如,查询时间段2021-03-24 10:12:12 到 2021-09-24 10:12:12,
可以用awk ‘/^2021-03-24 10:12:*/,/^2021-09-24 10:12:*/’ nginx.log查询。
边栏推荐
猜你喜欢

How can we make a monthly income of more than 10000? We media people with low income come and have a look

运动【跑步 01】一个程序员的半马挑战:跑前准备+跑中调整+跑后恢复(经验分享)

DM8 tablespace backup and recovery

What if the wireless network connection of the laptop is unavailable
![[CV] Wu Enda machine learning course notes | Chapter 9](/img/de/41244904c8853b8bb694e05f430156.jpg)
[CV] Wu Enda machine learning course notes | Chapter 9

C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,Gauss-Jordan消去法,源代码

ctfshow web255 web 256 web257

AcWing 244. Enigmatic cow (tree array + binary search)

How to solve the problem of computer jam and slow down

Moher College webmin unauthenticated remote code execution
随机推荐
Flutter integrated amap_ flutter_ location
C # implements a queue in which everything can be sorted
NewH3C——ACL
DM8 uses different databases to archive and recover after multiple failures
微服務入門:Gateway網關
[go basics] 1 - go go
Newh3c - network address translation (NAT)
deno debugger
Sort by item from the list within the list - C #
Use preg_ Match extracts the string into the array between: & | people PHP
Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
deno debugger
Codeforces Global Round 21(A-E)
Redis 哨兵机制
The basic syntax of mermaid in typera
Collections in Scala
ES6 summary
Developers really review CSDN question and answer function, and there are many improvements~
How to play dapr without kubernetes?
Guanghetong's high-performance 4g/5g wireless module solution comprehensively promotes an efficient and low-carbon smart grid