0

I have html templates and I want to get list of all template variables like :

<li>User ID <!--{ID}--></li> 

<!--{ID}--> is a template variables

Edit:

Regex pattern for doing this is :

\<\!--\{(.*?)\}--\>

I need to catch only unique occurrences for any variable e.g <!--{ID}--> might be present multiple time in template

sakhunzai
  • 13,900
  • 23
  • 98
  • 159

2 Answers2

1

With Perl:

perl -lne '{ while (/<!--{(.*?)}-->/g) {$t{$1}++}  } 
        END{ print for keys %t }' input.html

With less Perl statements:

perl -lpe '{s/.*?(<!--{[^}]*}-->)/\1\n/g}' input | \
        grep '<!--{' | sort | uniq
perreal
  • 94,503
  • 21
  • 155
  • 181
  • Would you like to suggest regex alternative ?. Thanks for the bash command that is perfect. – sakhunzai Jun 07 '13 at 05:04
  • are there multiple template parameters on a line? – perreal Jun 07 '13 at 05:07
  • Then it is not possible with sed and with simple regexes, I would go with the perl one-liner. – perreal Jun 07 '13 at 05:17
  • your first command is perfectly returning unique list of variables , but I also need a regex version to use within php e.g using preg_match_all().However, I ll accept the answer w.r.t your first command. Thanks – sakhunzai Jun 07 '13 at 05:25
0
ack -o '\<\!--\{(.*?)\}--\>' input.html | uniq -c
jaypal singh
  • 74,723
  • 23
  • 102
  • 147