LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   using sed replace command with HTML brackets (https://www.linuxquestions.org/questions/linux-newbie-8/using-sed-replace-command-with-html-brackets-931286/)

casperdaghost 02-25-2012 04:22 PM

using sed replace command with HTML brackets
 
I want to replace the angle brackets in this text.
Code:

<body>
<p>
Here is some normal text
</p>
<span class="bigText">Monster Text!</span>
<p>
Next to the big text
</p>

so i figures i would use the sed replace commands however it does not replae the while tag - I get <lt; - it is skipping the ampersand and retaining the angle bracket - not replacing it.
Code:

more more_body | sed -e 's/</&lt;/g'


<lt;body>

<lt;p>
Here is some normal text
<lt;/p>

<lt;span class="bigText">Monster Text!<lt;/span>

<lt;p>
Next to the big text
<lt;/p>

********

This is what I want it to look like :
Code:

&lt;body>

&lt;p>
Here is some normal text
&lt;/p>

&lt;span class="bigText">Monster Text!&lt;/span>

&lt;p>
Next to the big text
&lt;/p>

I want the less than angle brackets replaces with the the html entity - &lt;

Cedrik 02-25-2012 04:36 PM

& has special meaning in sed, you have to escape it
Code:

sed 's/</\&lt;/g' file...
man sed
Code:

...
s/regexp/replacement/
              Attempt  to match regexp against the pattern space.  If success-
              ful,  replace  that  portion  matched  with  replacement.    The
              replacement may contain the special character & to refer to that
              portion of the pattern space  which  matched,  and  the  special
              escapes  \1  through  \9  to refer to the corresponding matching
              sub-expressions in the regexp.



All times are GMT -5. The time now is 03:15 AM.