Wrapping text around a string with sed & functionality

Need to use sed to do a string replacement.

This:


# Comments until end of line


Should be:


<span class=comment>#Comments until end of line</span>


This can be accomplished with sed by using the ‘&’ operator which pastes in the match space.

 

Script started on Mon 01 Aug 2011 04:47:45 PM CDT

deadlycoffee:/tmp #  sed 's/^#.*.$/(&)' test | sed 's/^#$/(&)/' 
(#)
(#)
(#)
this is not a comment
this isn't either
(## This is)
(# So is this.)
deadlycoffee:/tmp #  exit 
exit

Script done on Mon 01 Aug 2011 04:48:52 PM CDT

 

In the above example, I do a much simpler replacement, but it is the same idea.

Leave a Reply

Your email address will not be published. Required fields are marked *