Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to remove comment tags in an html file.

<!--- <script save and execute me> -->

must become:

<script save and execute me>

I tried

sed -i s_^<!-- \(.*\) -->$_\1_ text.sed

but that fails because the < and > are considered to be read in/out characters. I than tried:

sed -i 's_^<!-- \(.*\) -->$_\1_' text.sed

but than the \1 is not evaluated as it should. Hopefully somebody here has ideas?

regards, Jeroen.

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Given a file, test.html, containing:

<html>
<!--- <script save and execute me> -->
</html>

The command:

sed -e "s/<!---* *<\(.*\)> *-->/<\1>/" test.html

emits:

<html>
<script save and execute me>
</html>

Be aware that this would also transform:

<html>
<!-- some info explaining why we have commented out the following -->
<!-- <hr> -->
<!--- <script save and execute me> -->
</html>

into:

<html>
<!-- some info explaining why we have commented out the following -->
<hr>
<script save and execute me>
</html>
share|improve this answer
1  
This (and every other attempt to parse HTML with sed) will fail spectacularly in so many different ways. I can count at least three different ways at a glance to make this fail. Regex is not for parsing HTML –  Darth Android Jul 7 '11 at 19:33
3  
The question wasn't about parsing HTML, it was about making a specific substitution in a file. The answer above isn't intended to do any more than solve the one specific problem. As far as it goes, SED is a fine tool for solving specific problems like this where the form of the input is clearly understood, regardless of the context. –  BillP3rd Jul 7 '11 at 19:46
    
Added additional example illustrating possible unintended consequences alluded to by @Darth Android. –  BillP3rd Jul 7 '11 at 19:57
    
@BillP3rd, thank you for your patience and your response to Darth Android. I'm more than aware that regex is not for parsing html but as you argued this is a very specific usecase for commenting out script references to local js files, and commenting in references to external js google api's –  dr jerry Jul 7 '11 at 21:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.