regex - extracting text between "[-" and "-]" using grep -
i have text file words/phrases tagged
[-sample text-]
or
{+sample text+}
how can use grep extract bracketed words in text?
use grep pcre support
you're describing appears word-diff format. difficult standard grep, easy more powerful regular expression engines pcre.
with pcre (perl-compatible regular expression) library compiled grep, or pcregrep binary, can use types of zero-width assertions in pattern. example, assuming have following file contents:
foo [-sample text 1-] bar {+sample text 2+} baz
you can correct result with:
$ pcregrep -o '(?<={\+|\[-).*?(?=\+}|-\])' /tmp/foo sample text 1 sample text 2
Comments
Post a Comment