当前位置:网站首页>How to use grep to find pattern matching across multiple lines
How to use grep to find pattern matching across multiple lines
2022-07-29 10:58:00 【mikes zhang】

grep Is a command line text search utility , Be able to find patterns and strings in files and other types of input . Most matches match only one line , But matching across multiple lines is often useful .
Use grep Match multiple new lines
It is difficult to deal with multi line matching grep . A better tool is awk or sed, Both of them can naturally handle multiple lines of input . Using two expressions with commas will match everything between these two patterns .
awk '/from/,/to/' file
sed -n '/from/,/to/p' file

Still can be in To deal with this problem grep, But the command is very clumsy .
grep -Pz '(?s)from.*n.*to' test

There are several things that have been done :
- -P open Perl Compatible regular expressions .
- -z Provide the entire file as a line , Use “ Zero byte ” Not line breaks . This allows grep Treat the whole thing as one line . (?s) open PCRE_DOTALL, send . Characters match any character , Include line breaks .
- from It's the starting game .
- .n. Will match everything , until to, This is the end match .
Overall speaking , This will be done for scripting purposes , But if you type it yourself , Please remember a lot . Besides , Use this -o The flag prints only matching items and also trailing zero byte characters , This can lead to other problems .
Use pcre2grep Instead of (Perl Compatible grep)
just grep Is not the best tool to complete this work , There's an alternative pcre2grep , Out of the box land support Perl Compatible regular expressions , And it can easily match multi line regular expressions .
It may already be installed on your system , But if not , You can get it from the package manager :
sudo apt install pcre2-utils
then , You only need to use -M Parameter to run it .
pcre2grep -M 'from(n|.)*to' file

Please note that , This still requires you to manually “ Newline or any character ” And (n|.)* . perhaps , You can use the (?s) Tips open PCRE_DOTALL And make the dot character match the newline character .
pcre2grep -M '(?s)from.*to' file
边栏推荐
- Alibaba architects spent a year sorting out the "Lucene advanced document", and you are also a big factory employee!
- 开源峰会抢先看 | 7 月 29 日分论坛 & 活动议程速览
- [unity, C #] character keyboard input steering and rotation
- LeetCode二叉树系列——144.二叉树的前序遍历
- Kunlunbase instruction manual (III) data import & synchronization
- JVM知识点详细整理(长文警告)
- 什么是 Kubernetes 自定义资源定义 (CRD)?
- 基于flask写的一个小商城mall项目
- R language Monte Carlo method and average method are used to calculate the definite integral. Considering the random point casting method, the confidence is 0.05, and the requirement is ϵ= 0.01, numbe
- Steps of project explanation in interview
猜你喜欢

浅谈安科瑞灭弧式智慧用电在养老机构的应用

DNS协议、ICMP协议、NAT技术

LeetCode二叉树系列——144.二叉树的前序遍历

Kunlun storage vs PostgreSQL OLTP test

如何在匹配行之前使用 grep 显示文件名和行号

开源峰会抢先看 | 7 月 29 日分论坛 & 活动议程速览

QT基本工程的解析

If distributed file storage is realized according to integrated Minio

重磅 | 基金会为白金、黄金、白银捐赠人授牌

Understand what a binary tree is (types, traversal methods, definitions of binary trees)
随机推荐
Start from scratch blazor server (3) -- add cookie authorization
LeetCode_1049_最后一块石头的重量Ⅱ
ES6 arrow function this points to
AI model risk assessment Part 2: core content
Luogu p4185 [usaco18jan]mootube g problem solution
Structure the eighth operation of the combat battalion module
Learning R language these ebooks are enough!
深入理解C# 可空类型
就这?TypeScript其实并不难!(建议收藏)
Conference OA project - my approval
3.认识和操作一下mysql的基本命令
leetcode-位运算
Using xgboost with tidymodels
PHP basics uses arrays to save data
SkiaSharp 之 WPF 自绘 弹动小球(案例版)
深度强化学习应用实践技巧
为什么应该在开发环境中使用 Kubernetes
浅谈string中的compareTo方法
Alibaba P8 broke out this interview guide for big factories. After reading it, the salary soared by 30K!
Zhou Hongyi: 360 is the largest secure big data company in the world