I want to match all patterns that start with [%
and end with %]
in a file.
I've tried multiple tools such as awk, sed, pcregrep and none of them seem to work, although they are suggested as top answers on similar questions.
[% FOREACH selection = selections -%]
case SELECTION_ID_[% SELECTION_NAME %]: {
const [% selectionType %]& source = this->[% selectionName %]();
rc = bcem_AggregateUtil::toAggregate(result,
d_selectionId,
source);
} break;
[% END -%]
[% foo ]
[% INCLUDE attributeSearchBlock
tree=attributeSearchTree depth=0
visit='ReturnAttributeInfo' name='name' nameLength='nameLength' -%]
For the code above, I expect the following result:
[% FOREACH selection = selections -%]
case SELECTION_ID_[% SELECTION_NAME %]: {
const [% selectionType %]& source = this->[% selectionName %]();
[% END -%]
[% INCLUDE attributeSearchBlock
tree=attributeSearchTree depth=0
visit='ReturnAttributeInfo' name='name' nameLength='nameLength' -%]
But I am getting all the lines matched instead.
What am I doing wrong?
LATER EDIT:
If it's on multiple lines, it should also be matched. For example:
[% foo
bar -%]
LATER EDIT 2: None of the answers seems to work, so I did the whole thing manually using the following:
hasPatternStarted=false
while read -r line; do
if [[ $line =~ '[%' ]]; then
hasPatternStarted=true
fi
if [[ $line =~ '%]' ]]; then
hasPatternStarted=false
echo $line
fi
if [ "$hasPatternStarted" = true ]; then
echo $line
fi
done < "$filename"
It works fine, but if anyone has a one liner to solve this problem (using sed, awek, pcregrep, perl, grep anything), please say so.