Tuesday 15 June 2010

regex - Sed and Awk Escaping Ampersands (&) -


I am parsing a PHP file and wrapping the function prototype element in HTML. If amps, it breaks my code.

Input : string in the form of fu (& $ var1, & $ var2) {...} // Desired output (in HTML) : & amp; $ Var1, & amp; $ Var2 // Basically, only the output of the variable so that they appear properly in the browser

Right now, I will assign each variable to awk sub method < Sup> 1 , and then for SAD

  Sub (/ ^ & amp; /, "\\\ & amp;", Ultimate) # is the variable of interest (e.g. & amp; $ var1) #Intermediate step case is relevant Awk-processed element #are has been sent to $ {file} _param.txt. Each set of parameters has been delineated by the colon. Ultimate = $ (Cut-D: -f $ counter $ {file} _param.txt) Replace some default text in template file with real stuff Sed -i "s | @PARam | $ Ultimate | 1" "$ base" _funct_def.txt   

I am getting the output: The interpretation of the ampersand is being interpreted. The whole match is changed.

Isolation of problem: In the browser instead of 'g $ var1' displays the following because I want it, however, I have a & amp; instead of this.

  sub (/ ^ & amp; /, "g", ultimate)   

My attempt: Because I have all three backslashes I thought that before awk 'it & amp; Which, fed in sed, '& amp; Literal '& amp;' As

Question: How can I escape from &?

1 manual:


some "meta" / design questions what i try to do I am (not necessarily a question!)
I have a bunch of PHP files which I am trying to prepare for some documents for Zdocs. I am going through the REGEX and Shell scripts and I am parsing them, so that I can list the function name, parameter and return item (s). So far, Regx has worked very well for me, but I have read a lot about how this is something that the Reggae should not be used. I welcome any comment about any of these (how usually does documentation occur?) Thanks guys!

I believe that HTML has and & amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Character. You can use it in your Akki script:

  sub (/ ^ & amp; /, "& amp;", ultimate)   

Dollar sign-in ultimate is required to avoid a backslash, eg & Amp; $ Var & amp; \ $ Var, or sed and awk should try to expand $ var as a variable.

No comments:

Post a Comment