среда, 28 августа 2013 г.

Multiline regex

The simplest solution would be to use pcregrep, that is grep using the pcre library. Use the flag -M for multiline search:
pcregrep -M 'expression1\nexpression2' filename
if you'd like more 'native' solution without installing additional packages, there's a Python one-liner that will do the job:
alias mlgrep='python -c '\''import re, sys; \
    sys.exit("Usage: mlgrep PATTERN") if len(sys.argv) != 2 else True; \
    match = re.findall(r".*" + sys.argv[1] + r".*", sys.stdin.read().strip(), re.MULTILINE); \
    print("\n".join([x.strip() for x in match])) if match else sys.exit(1)'\'''
add this line to your ~/.bashrc file and use it as:
cat filename | mlgrep "expression1\nexpression2"

Комментариев нет:

Отправить комментарий