linux - [Solved-4 Solutions] How to search for a multiline pattern in a file ? - ubuntu - red hat - debian - linux server - linux pc



Linux - Problem :

How to search for a multiline pattern in a file ?

Linux - Solution 1:

For example, you need to find files where the '_name' variable is immediatelly followed by the '_description' variable:

find . -iname '*.py' | xargs pcregrep -M '_name.*\n.*_description'
click below button to copy the code. By - Linux tutorial - team

Tip: you need to include the line break character in your pattern. Depending on your platform, it could be '\n', \r', '\r\n', ..

Linux - Solution 2:

awk '/Start pattern/,/End pattern/' filename
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 3:

grep -Pzo '_name.*\n.*_description'
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 4:

grep -P also uses libpcre, but is much more widely installed. To find a complete title section of an html document, even if it spans multiple lines, you can use this:

grep -P '(?s)<title>.*</title>' example.html
click below button to copy the code. By - Linux tutorial - team

Since the PCRE project implements to the perl standard, use the perl documentation for reference:


Related Searches to - linux - linux tutorial - How to search for a multiline pattern in a file ?