当前位置:网站首页>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查询。
边栏推荐
- 1. Kalman filter - the best linear filter
- Put a lantern on the website during the Lantern Festival
- C#实现一个万物皆可排序的队列
- ArcGIS application (XXII) ArcMap loading lidar Las format data
- 1. Qt入门
- awk从入门到入土(14)awk输出重定向
- Comprendre la méthode de détection des valeurs aberrantes des données
- How to get bytes containing null terminators from a string- c#
- ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据
- Using the rate package for data mining
猜你喜欢
From scratch, use Jenkins to build and publish pipeline pipeline project
Basic operations of databases and tables ----- view data tables
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
【性能測試】一文讀懂Jmeter
4 small ways to make your Tiktok video clearer
Codeforces Round #793 (Div. 2)(A-D)
What if the wireless network connection of the laptop is unavailable
Snipaste convenient screenshot software, which can be copied on the screen
1. Getting started with QT
随机推荐
Unity text superscript square representation +text judge whether the text is empty
Using the rate package for data mining
09 softmax regression + loss function
Flutter 集成 amap_flutter_location
ctfshow web255 web 256 web257
Add log file to slim frame - PHP
Group programming ladder race - exercise set l2-002 linked list de duplication
Const string inside function - C #
【性能測試】一文讀懂Jmeter
如何通过antd的upload控件,将图片以文件流的形式发送给服务器
Chrome is set to pure black
Figure guessing game
转:优秀的管理者,关注的不是错误,而是优势
Démarrage des microservices: passerelle
Need help resetting PHP counters - PHP
Sports [running 01] a programmer's half horse challenge: preparation before running + adjustment during running + recovery after running (experience sharing)
Four essential material websites for we media people to help you easily create popular models
Redis sentinel mechanism
[performance test] read JMeter
Newh3c - routing protocol (RIP, OSPF)