For the reason see
http://en.wikipedia.org/wiki/XML#Escaping
Code:
#!/bin/sh
# --- replace
# '&' with '&' : 's~\&~\&~g'
# '<' with '<' : 's~<~\<~g'
# '>' with '>' : 's~>~\>~g'
# '"' with '"' : 's~\"~\"~g'
# --- First replace all existing '&' signs.
# --- Prevents an original '<' first changed into '<' ending up as '&lt;'
sed -e 's~&~\&~g' -e 's~<~\<~g' -e 's~>~\>~g'
# 's~&~\&~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:
36 <code>
37 #!/bin/sh
38
39 # --- replace
40 # '&' with '&' : 's~\&~\&~g'
41 # '<' with '<' : 's~<~\<~g'
42 # '>' with '>' : 's~>~\>~g'
43 # '"' with '"' : 's~\"~\"~g'
44
45 # --- First replace all existing '&' signs.
46 # --- Prevents an original '<' first changed into '<' ending up as '&lt;'
47
48 sed -e 's~&~\&~g' -e 's~<~\<~g' -e 's~>~\>~g'
49
50 # 's~&~\&~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 # '&' 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
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