当前位置:网站首页>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查询。
边栏推荐
- 转:优秀的管理者,关注的不是错误,而是优势
- Educational Codeforces Round 115 (Rated for Div. 2)
- Call Baidu map to display the current position
- Question 49: how to quickly determine the impact of IO latency on MySQL performance
- 运动【跑步 01】一个程序员的半马挑战:跑前准备+跑中调整+跑后恢复(经验分享)
- SSRF vulnerability exploitation - attack redis
- Use preg_ Match extracts the string into the array between: & | people PHP
- How to send pictures to the server in the form of file stream through the upload control of antd
- [attack and defense world | WP] cat
- How to use C language code to realize the addition and subtraction of complex numbers and output structure
猜你喜欢

OpenFeign 服务接口调用

1、卡尔曼滤波-最佳的线性滤波器

1. Qt入门

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

4 small ways to make your Tiktok video clearer

Redis 哨兵机制

Comprendre la méthode de détection des valeurs aberrantes des données

How to play dapr without kubernetes?

NPM run build error

High order phase difference such as smear caused by myopic surgery
随机推荐
Learn nuxt js
DM8 uses different databases to archive and recover after multiple failures
Use preg_ Match extracts the string into the array between: & | people PHP
Snipaste convenient screenshot software, which can be copied on the screen
Using the rate package for data mining
@Role of requestparam annotation
1. Getting started with QT
PHP converts seconds to timestamps - PHP
ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据
Chrome is set to pure black
Cancel ctrl+alt+delete when starting up
NPM run build error
2022 tower crane driver examination and tower crane driver examination questions and analysis
根据数字显示中文汉字
FOC control
DM8 tablespace backup and recovery
How to solve the problem of computer jam and slow down
Educational Codeforces Round 119 (Rated for Div. 2)
DM8 command line installation and database creation
Turn: excellent managers focus not on mistakes, but on advantages