I am trying to make a fairly simple string analysis in a bash script. Essentially, I have a file that consists of several multi-line fields. Each field is surrounded by a well-known header and footer.
I want to extract each field separately into an array or similar, like this
>FILE='cat file'
>REGEX="@#@#@#[\s\S]+?@#@#@"
>
>if [[$FILE =~ $REGEX ]] then
> echo $BASH_REMATCH
>fi
FILE:
@
this is field one
@
@
this is field two
they can be any number of lines
@
Now I'm sure the problem is that bash does not match newlines with "."
I can match this with "pcregrep -M", but of course the whole file will match. Can I get one match at a time from pcregrep?
I do not mind using embedded Perl or similar.
source
share