I'm searching a directory (and deeper directories) for twig files that have variables {{ something var name }}
and I would like to output into a file (twig-vars.txt).
Maybe using grep
and regex
? This is not my wheelhouse, so thanks in advance!
Example output (twig-vars.txt):
{{ somevar }}
{{ someothervar }}
{{ site.url }}
{{ ... }}
CodePudding user response:
With grep
in PCRE
mode:
grep -oP '{{\s*\K[\w.] ' file
-------8<------------------
somevar
someothervar
site
Or with Perl:
perl -nE 'say $& if/{{\s*\K[\w.] /' file
-------8<------------------
somevar
someothervar
site
The regular expression matches as follows:
Node | Explanation |
---|---|
{{ |
'{{' |
\s* |
whitespace (\n, \r, \t, \f, and " ") (0 or more times (matching the most amount possible)) |
\K |
resets the start of the match (what is K ept) as a shorter alternative to using a look-behind assertion: look arounds and Support of K in regex |
[\w.] |
any character of: word characters (a-z, A- Z, 0-9, _), '.' (1 or more times (matching the most amount possible)) |
Or with awk
awk -F'{{ | }}' '{print $2}' file
-------8<------------------
somevar
someothervar
site.url