View Single Post
  #1   (View Single Post)  
Old 3rd December 2009
J65nko J65nko is offline
Administrator
 
Join Date: May 2008
Location: Budel - the Netherlands
Posts: 4,128
Default A script to escape the reserved XML characters

For the reason see http://en.wikipedia.org/wiki/XML#Escaping

Code:
#!/bin/sh

# --- replace
# '&' with '&'      :       's~\&~\&~g'
# '<' with '&lt;'       :       's~<~\&lt;~g'
# '>' with '&gt;'       :       's~>~\&gt;~g'
# '"' with '&quot'      :       's~\"~\&quot;~g'

# --- First replace all existing '&' signs.
# --- Prevents an original '<' first changed into '&lt;' ending up as '&amp;lt;'

sed -e 's~&~\&amp;~g' -e 's~<~\&lt;~g'  -e  's~>~\&gt;~g'

#  's~&~\&amp;~g'
#
#       s               search and replace
#       ~               our custom delimiter indicating start of search pattern
#       &               a '&' 
#       ~               end of search pattern, start of replacement
#
#       \&              a "&" escaped with "\" ( in replacement "&" means: matched pattern )
#       amp             followed by a 'a', 'm' and 'p'
#       ;               and a ';'
#
#       ~               end of replacement
#       g               the search and replace globally: don't stop at first occurrence,
#                       but check the whole line
A complete file could be converted with:
Code:
$ esc-xml <infile.txt >outfile.xml
A example of using 'esc-xml' from within vi:
  • enter command mode with ESC
  • type a ':'
  • the line range
  • a '!' followed by the name of the script

In the follpwing screenshot the script has been pasted into a text file and we will have it operate on itself with:
Code:
37,62!esc-xml
Code:
     36 <code>
     37 #!/bin/sh
     38 
     39 # --- replace
     40 # '&' with '&amp;'      :       's~\&~\&amp;~g'
     41 # '<' with '&lt;'       :       's~<~\&lt;~g'
     42 # '>' with '&gt;'       :       's~>~\&gt;~g'
     43 # '"' with '&quot'      :       's~\"~\&quot;~g'
     44 
     45 # --- First replace all existing '&' signs.
     46 # --- Prevents an original '<' first changed into '&lt;' ending up as '&amp;lt;'
     47 
     48 sed -e 's~&~\&amp;~g' -e 's~<~\&lt;~g'  -e  's~>~\&gt;~g'
     49 
     50 #  's~&~\&amp;~g'
     51 #
     52 #       s               search and replace
     53 #       ~               our custom delimiter indicating start of search pattern
     54 #       &               a '&'
     55 #       ~               end of search pattern, start of replacement
     56 #
     57 #       \&              a "&" escaped with "\" ( in replacement "&" means: matched patt
ern )
     58 #       amp             followed by a 'a', 'm' and 'p'
     59 #       ;               and a ';'
     60 #
     61 #       ~               end of replacement
     62 #       g               the search and replace global
     63 </code>
     64
~
:37,62!esc-xml
After pressing 'ENTER' the result is:
Code:
     36 <code>
     37 #!/bin/sh
     38
     39 # --- replace
     40 # '&amp;' with '&amp;amp;'      :       's~\&amp;~\&amp;amp;~g'
     41 # '&lt;' with '&amp;lt;'       :       's~&lt;~\&amp;lt;~g'
     42 # '&gt;' with '&amp;gt;'       :       's~&gt;~\&amp;gt;~g'
     43 # '"' with '&amp;quot'      :       's~\"~\&amp;quot;~g'
     44
     45 # --- First replace all existing '&amp;' signs.
     46 # --- Prevents an original '&lt;' first changed into '&amp;lt;' ending up as '&amp;amp;lt;'
     47
     48 sed -e 's~&amp;~\&amp;amp;~g' -e 's~&lt;~\&amp;lt;~g'  -e  's~&gt;~\&amp;gt;~g'
     49
     50 #  's~&amp;~\&amp;amp;~g'
     51 #
     52 #       s               search and replace
     53 #       ~               our custom delimiter indicating start of search pattern
     54 #       &amp;               a '&amp;'
     55 #       ~               end of search pattern, start of replacement
     56 #
     57 #       \&amp;              a "&amp;" escaped with "\" ( in replacement "&amp;" means: matched
pattern )
     58 #       amp             followed by a 'a', 'm' and 'p'
     59 #       ;               and a ';'
     60 #
     61 #       ~               end of replacement
     62 #       g               the search and replace global
     63 </code>
26 lines added; 26 lines deleted
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump
Reply With Quote