I need to extract all host variables from a SQL statement that are part of a concatenation. Example input:
select * from table where :first-var || :second-var
From this I need to extract 'first-var' and 'second-var'.
Matching one or the other can be done using:
grep -o -E ':\S+\s+\|\|'
matches ':first-var ||' and
grep -o -E '\|\|\s+:\S+'
matches '|| :second-var'
However when I combine these two expressions in an alternation pattern only one result is returned:
grep -o -E '\|\|\s+:\S+|:\S+\s+\|\|'
Splitting the command up in multiple patterns also matches only one result:
grep -o -E -e '\|\| :second-var' -e ':first-var \|\|'
I suspect that the pipe symbols are "used up" after the first match because the following does return both results:
grep -o -E -e '\| :second-var' -e ':first-var \|'
How can I get all the matches? Note that the concatenation symbols do not need to be part of the output, I'm only interested in 'first-var' and 'second-var' in this example.