Linux Command Line – Tools for Multi-Line Regex Expressions

command linelinuxregular expression

I'm wanting to find the results of a multi-line regular expression in linux. I tried grep, but like most linux utilities it's line based.

Is there something similar that allows me to search across multiple lines and simply output the matches?

Not perl, awk etc, unless I can give it a regex directly.

The problem

I've done svn propget –recursive on my subversion repositories and now want to extract all trunk entries, which would require a regex of the form ^http[^ ]trunk.$$ (where it starts at "http" at the start of the line, and finishes at an empty line.

Best Answer

I don't know offhand the exact format you're trying to parse. If the http is always at the beginning of a paragraph (where paragraphs are separated by one or more blank line), you can invoke awk in paragraph mode (each paragraph is one record) and print records that begin with http. Perl has a similar mode.

awk -vRS= '/^http[^ ]/'
perl -000 -ne 'print if /^http[^ ]/'

If there isn't always a blank line before http, you can start printing when you see http and stop at a blank line. For example:

awk '/^http[^ ]/, /^$/'
perl -ne 'print if /^http[^ ]/../^$/'
Related Question