当前位置:网站首页>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 college students choose suitable computers
- Flutter integrated amap_ flutter_ location
- Xcode 6 swift code completion does not work properly - Xcode 6 swift code completion not working
- FOC control
- AcWing 244. Enigmatic cow (tree array + binary search)
- C # implements a queue in which everything can be sorted
- R language uses cforest function in Party package to build random forest based on conditional inference trees, uses varimp function to check feature importance, and uses table function to calculate co
- Educational Codeforces Round 115 (Rated for Div. 2)
- Question 49: how to quickly determine the impact of IO latency on MySQL performance
- Four essential material websites for we media people to help you easily create popular models
猜你喜欢

【性能測試】一文讀懂Jmeter

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

Educational Codeforces Round 119 (Rated for Div. 2)

ctfshow web255 web 256 web257

Démarrage des microservices: passerelle

How to re enable local connection when the network of laptop is disabled

Educational Codeforces Round 115 (Rated for Div. 2)

High order phase difference such as smear caused by myopic surgery

NPM run build error

DM8 uses different databases to archive and recover after multiple failures
随机推荐
The second session of the question swiping and punching activity -- solving the switching problem with recursion as the background (I)
[CV] Wu Enda machine learning course notes | Chapter 9
How to play dapr without kubernetes?
MySQL relearn 1-centos install mysql5.7
std::is_ union,std::is_ class,std::integral_ constant
Flutter integrated amap_ flutter_ location
Parallel shift does not provide any acceleration - C #
Unity write word
Mouse over to change the transparency of web page image
The upper layer route cannot Ping the lower layer route
[go basics] 1 - go go
From scratch, use Jenkins to build and publish pipeline pipeline project
Group programming ladder race - exercise set l2-002 linked list de duplication
PHP session variable passed from form - PHP
User login function: simple but difficult
[untitled] 2022 polymerization process analysis and polymerization process simulation examination
Conversion of yolov5 XML dataset to VOC dataset
How college students choose suitable computers
根据数字显示中文汉字
Group programming ladder race - exercise set l1-006 continuity factor