当前位置:网站首页>Awk from entry to penetration (6) regular matching
Awk from entry to penetration (6) regular matching
2022-07-04 08:46:00 【Wonderful binary】
awk Of pattern In addition to using simple conditional expressions , You can also use regular expressions .
AWK Regular expressions can be processed easily and efficiently . A large number of complex tasks can be solved by extremely simple regular expressions . Everyone who is proficient in the command line knows the real power of regular expressions .
This chapter will focus on the use of standard regular expressions .awk The regularity of is basically the same as that of ordinary regularity , But in awk You need to use two regular expressions in / wrap up .
spot (Dot)
Dot character (.) You can match all characters except the end of line character . For example, the stool below can match fin, fun, fan wait .
$ echo -e "cat\nbat\nfun\nfin\nfan" | awk '/f.n/'
Executing the above command can get the following results :
fun
fin
fan
OK, let's start
Line start (^) Match the beginning of a line . The following example will output all the characters as strings The The first line .
$ echo -e "This\nThat\nThere\nTheir\nthese" | awk '/^The/'
You can get the following result by executing the above command :
There
Their
End of line
Line end ($) Match the end of a line . In the following example, all characters will be output n The ending line :
$ echo -e "knife\nknow\nfun\nfin\nfan\nnine" | awk '/n$/'
You can get the following result by executing the above command :
Ca
fin
fan
Match character set
Matching character sets are used to match sets ( Denoted by square brackets ) A character in . In the following example , matching Call And Tall It doesn't match Ball.
$ echo -e "Call\nTall\nBall" | awk '/[CT]all/'
You can get the following result by executing the above command :
Call
Tall
Exclusion set
Regular matching excludes characters from the set . In the following example, only Ball.
$ echo -e "Call\nTall\nBall" | awk '/[^CT]all/'
You can get the following result by executing the above command :
Ball
or
A vertical bar (|) Allow regular expressions to implement logic or operations . The following example will output Ball And Call .
$ echo -e "Call\nTall\nBall\nSmall\nShall" | awk '/Call|Ball/'
You can get the following result by executing the above command :
Call
Ball
Once at most
The symbol ( ?) The preceding character does not appear or appears once . The following example matches Colour And Color. Use ? bring u Becomes an optional character .
$ echo -e "Colour\nColor" | awk '/Colou?r/'
You can get the following result by executing the above command :
Colour
Color
Zero or more times
The symbol (*) Allow the character before it to appear multiple times or not . The following example will match ca,cat, catt wait .
$ echo -e "ca\ncat\ncatt" | awk '/cat*/'
You can get the following result by executing the above command :
ca
cat
catt
One or more times
The symbol (+) Make the character before it appear one or more times . The following example will match a 2 Or multiple consecutive 2.
$ echo -e "111\n22\n123\n234\n456\n222" | awk '/2+/'
You can get the following result by executing the above command :
22
123
234
222
grouping
Brackets are used for grouping and characters | Used to provide a variety of options . The following regular expression will match all that contain Apple Juice or Aplle Cake The line of .
$ echo -e "Apple Juice\nApple Pie\nApple Tart\nApple Cake" | awk '/Apple (Juice|Cake)/'
You can get the following result by executing the above command :
Apple Juice
Apple Cake
Multiple expressions can use && perhaps , come together , The meaning of and :
awk ‘/^ Start time date /,/^ End time date /’ nginx.log
such as , Query time period 2021-03-24 10:12:12 To 2021-09-24 10:12:12,
It can be used awk ‘/^2021-03-24 10:12:*/,/^2021-09-24 10:12:*/’ nginx.log Inquire about .
边栏推荐
- The basic syntax of mermaid in typera
- How college students choose suitable computers
- 没有Kubernetes怎么玩Dapr?
- Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
- DM8 uses different databases to archive and recover after multiple failures
- deno debugger
- How does Xiaobai buy a suitable notebook
- HMS core helps baby bus show high-quality children's digital content to global developers
- Need help resetting PHP counters - PHP
- Technology sharing | MySQL parallel DDL
猜你喜欢

Bishi blog (13) -- oral arithmetic test app

Codeforces Round #803 (Div. 2)(A-D)

Educational Codeforces Round 119 (Rated for Div. 2)

Conversion of yolov5 XML dataset to VOC dataset

What sparks can applet container technology collide with IOT

Take you to master the formatter of visual studio code

Mouse over to change the transparency of web page image

广和通高性能4G/5G无线模组解决方案全面推动高效、低碳智能电网

FOC control

High order phase difference such as smear caused by myopic surgery
随机推荐
es6总结
转:优秀的管理者,关注的不是错误,而是优势
Guanghetong's high-performance 4g/5g wireless module solution comprehensively promotes an efficient and low-carbon smart grid
Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
Codeforces Global Round 21(A-E)
awk从入门到入土(12)awk也可以写脚本,替代shell
Parallel shift does not provide any acceleration - C #
awk从入门到入土(7)条件语句
awk从入门到入土(14)awk输出重定向
Mouse over to change the transparency of web page image
Laravel page load problem connection reset - PHP
Educational Codeforces Round 119 (Rated for Div. 2)
Need help resetting PHP counters - PHP
Difference between static method and non static method (advantages / disadvantages)
Manjaro install wechat
awk从入门到入土(9)循环语句
Redis 哨兵机制
Getting started with microservices: gateway gateway
1. Getting started with QT
[attack and defense world | WP] cat