Note that there are some explanatory texts on larger screens.

plurals
  1. POSed and Awk Escaping Ampersands (&)
    text
    copied!<p>I'm parsing a PHP file and wrapping function prototype elements in HTML. If there are ampersands however, it breaks my code.</p> <p><strong>Input</strong>: <code>function foo (&amp;$var1, &amp;$var2){...}</code>//as String<br> <strong>Desired output (in HTML)</strong>: <code>&amp;$var1, &amp;$var2</code> <em>//basically, just output the variables so that they are properly displayed in a browser</em></p> <p>Right now, I am sending each variable into awk's sub method<sup><b>1</b></sup>, and then to sed.</p> <pre><code>sub(/^&amp;/, "\\\&amp;", param) #param is the variable of interest (e.g. &amp;$var1) #Intermediate step in case it's relevant. The awk-processed elements #are sent to ${file}_param.txt. Each set of parameters are delimited by colons. param=$(cut -d: -f$counter ${file}_param.txt) #Replace some default text in template file with real stuff. sed -i "s|@PARAM|$param|1" "$base"_funct_def.txt </code></pre> <p><strong>Output I'm getting:</strong> The ampersands are being interpreted. The entire match is replaced.</p> <p><strong>Isolation of issue:</strong> Doing the following instead displays 'g$var1' in the browser as I want it to. However, I'm trying to get an '&amp;' instead.</p> <pre><code>sub(/^&amp;/, "g", param) </code></pre> <p><strong>My attempts:</strong> I used three backslashes because I thought awk would first process it into '\&amp;' which, fed into sed, would interpret '\&amp;' as the literal '&amp;'. I have tried anywhere from 1 to 6 backslashes though, to no avail.</p> <p><strong>QUESTION:</strong> How can I escape the &amp;?</p> <p><sup><b>1</b></sup>Manual: <a href="http://www.staff.science.uu.nl/~oostr102/docs/nawk/nawk_92.html" rel="noreferrer">http://www.staff.science.uu.nl/~oostr102/docs/nawk/nawk_92.html</a></p> <hr> <p><em>Some "meta"/design questions about what I'm trying to do (not needed for question!!)</em><br> I have bunch of PHP files that I'm trying to generate a bit of documentation for (structured like Javadocs). I'm going through and parsing it using REGEX and shell scripts, so that I list out the function name, parameters, and return item(s). So far, REGEX has worked out pretty well I think, but I have read a lot about how this is something that REGEX should NOT be used for. I'd welcome any comments about any of this (how is documentation usually generated?). Thanks guys!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload