0

I have a GNU makefile with one of the recipe lines reading:

sed -i 's|<span class="math">$$\(.*\)$$</span>|<span style="font-size:100%">'"$$(curl -d "type=tex&q=\1" http://localhost:16000/)"'</span>|g' $(NAME).a4.xhtml

The idea is to replace <span class="math">$$\(.*\)$$</span>

by <span style="font-size:100%">..SVG..</span>

where ..SVG.. is the string returned by executing the shell command curl -d "type=tex&q=\1" http://localhost:16000/ and where \1 should be the previously remembered pattern \(.*\).

Problem: Both the pattern detection and the shell execution are working alright. However, \1 is not substituted by the previously remembered pattern.

PS: If one prefers to use sed with the -r option, matching inside the makefile is as follows:

sed -i -r 's@<span class="math">\$$(.*)\$$</span>@..@..' $(NAME).a4.xhtml
Serge Stroobandt
  • 28,495
  • 9
  • 107
  • 102

2 Answers2

0

The problem is that you are using shell substitution within sed which obviously will not expand \1 to the group match. One solution is to use sed's e command, a good explanation is given here.

If you need an example, refer the answer of this.

Community
  • 1
  • 1
Kannan Mohan
  • 1,810
  • 12
  • 15
0

While I suppose it might be possible to pull this off with GNU sed with the /e extension, may I humbly suggest you use a different tool?

perl -i -MLWP::UserAgent -pe '
  BEGIN { $u = LWP::UserAgent->new(); }
  s|<span class="math">\$\$\(.*?\)\$\$</span>|
    sprintf q{<span style="font-size:100%%">%s</span>},
      $u->post("http://localhost:16000/",
        [type=>"tex", q=>"$1"])->decoded_content()
  |ge' $(NAME).a4.xhtml

(Hoping there's a simpler way to POST from Perl but just grabbed what I could find. Maybe look at WWW::Mechanize instead if you have that.)

tripleee
  • 175,061
  • 34
  • 275
  • 318