Skip to main content

Print lines that matches a regex starting from a line that matches another regex until an until a line that matches a different regex

· One min read
Hreniuc Cristian-Alexandru

Print lines that matches a regex starting from a line that matches another regex until an until a line that matches a different regex:

cat file | awk '/startling_regex_excluded/{next} /printable_regex/{print} /until_regex/{exit}'

Having this file:

text
text2
startling_regex_excluded
printable_regex
printable_regex
printable_regex
until_regex
test
printable_regex
printable_regex

The output will be:

printable_regex
printable_regex
printable_regex

AWK CheatSheet

Get all lines starting from a position:

awk '/start line/{flag=1;next}flag' file.log