tags:

views:

66

answers:

3

I have nearly 100 HTML files that uses the <tt> tag to markup inline code which I'd like to change to the more meaningful <code> tag. I was thinking of doing something on the order of a massive sed -i 's/<tt>/<code>/g' command but I'm curious if there's a more appropriate industrial mechanism for changing tag types on a large HTML tree.

+2  A: 

If your are on a linux environment then sed is very easy, short, and fast way to do it.

Corrected command :

SAVEIFS=$IFS
IFS="\n"

for f in `find . -name "*.htm"` do sed -i 's/tt>/code>/g' "$f" ;done

IFS=$SAVEIFS

Some text editors or IDE also allow you to do a search and replace in directories with a filter on filename.

Loïc Février
It will not work if you have empty chars within filename. To avoid that use for i in $(find . -name "*.htm")...
Gadolin
Thank you. Just ran it and everything is fine. :)
Rich
@Gadolin $() will also not work. Program corrected with the use of $IFS.
Loïc Février
+1, I am surprised recently I started to use the form I put and thought it solves the problem. Just tested and you are right. Thx.
Gadolin
$() and `` are basically the same but the former one allow nested stuff (cool) and is cleaner especially with command with a lot of " / ' . Less quotes => more readable. I should have used $() but got used to the other one ;)
Loïc Février
What does the backspace do in `IFS`? Also, you can do `IFS=$'\n'` without needing to use `echo`. If you use `find|while read` you don't have to use `IFS` to accomodate filenames that may include spaces. @Gadolin: By "empty chars" do you mean "spaces" ("empty" implies "null" to me)?
Dennis Williamson
@Dennis Williamson : it was some code copied from an old script, I know it was working so I copied it. I will update the code.
Loïc Février
A: 

Hi,

For one time performance of such tasks I use UltraEdit on Windows. UE has a find and replace in files function that works great for this. I point it at the top of the directory tree containing the files I want to change, tell it to process sub-directories, give it the extension of the files I want to change, tell it what to change and what to change it to and go.

If you have to script this in linux, then I think the sed solution or a perl / php script will work great.

ISDi
+3  A: 

The nicest thing you may do is to use
xmlstartlet:
xml ed -r //b -v code

It is freaky powerful. See http://xmlstar.sourceforge.net/, http://www.ibm.com/developerworks/library/x-starlet.html

Gadolin
Wow thanks for posting. This is what I was really looking for.
Rich
note that on some no-so-valid html, xmlstarlet won't work. In this case use xml tr with the --html option. You will need to provide some xsl, which will be something like <xsl:template match="tt"><code><xsl:value-of select="."/></code></xsl:template>
hellvinz