[CS-FSLUG] I need a quick way to edit quite a few html documents

Tim Young Tim.Young at LightSys.org
Wed Jun 8 09:24:51 CDT 2011



On 6/8/2011 9:04 AM, l4c wrote:
> On 06/08/2011 09:58 AM, davidm at hisfeet.net wrote:
>> My web site has gotten badly out dated from lack of maintainence. 
>> I want
>> to get it back in shape.  One of the most immediate problems is that
>> (since we added a blog, and gave it the address that was the 
>> address of
>> the whole web site) every "home button" in every html file instead of
>> leading back to the old "home page", leads back to the blog.
> for file in *.html; do sed -i 's/blogpage/homepage/g' $file; done

I would like to second this, but add a little more information.  If 
you are not experienced in sed, this may give you some issues without 
more knowledge.

Sed is a pattern matching/replacing program.  Basically, the "sed -i" 
command does an in-place change of the files.  It reads the file, 
makes a change, and writes back to the original file.

The "*for file in *.html;*" portion will find all the html files in 
the current directory, and then do the sed edit on each one 
individually.  If you have multiple subdirectories, you will either 
need to run this command on all of the subdirectories or use "find" 
or something similar to do it for all of them.

The sed command itself, while fairly straight-forward, can give you 
grief if you have any path-parts to your link. For example, if you 
were trying to match "/Dave/blog" and replace it with 
"/newpage/index.html" you will run into some issues.  The "/" 
character is the magical separator, and needs to be prefixed with a 
back-slash.  So you would need to have the sed command be:
    's*/*\/Dave\/blog*/*\/newpage\/index.html*/'
*I am hoping that the bolded slashes show off.  The bolded slashes 
are the slashes that have not been escaped, and are the separators.  
They designate the two sections, a "match this" section, and a 
"replace it with this" section.

One problem with the sed -i solution is that it is permanent, 
regardless of what you intended.  There is no "undo" button.  You 
will want to make a backup of your files before you do it.

But, when you are simply doing a one-to-one replacement, it is a very 
simple and effective thing to do.  If you are struggling with this, 
drop us an email with the text of the actual href you would like to 
replace, and what you would like to change it to.

     - Tim




More information about the Christiansource mailing list