Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 1.00 average. Display Modes
Old 02-14-2005, 03:34 PM   #1
froinds
Triple-A Player
 
Join Date: May 2004
Posts: 158
sed maybe? replacing a word in many docs

Hi
I got this website that I'm hosting in our own servers. It runs on apache. The designer who created it made all the links and the html files with the extention .htm and not .html. In order for this to work, I had to change the apache configuration to look for index.htm instead of the default index.html.
Since we're gonna have another site (virtual host on the same computer) made by another designer who uses the extentions .html, I would like to replace every occurrence in all html files, inside the first site's folder, from .htm to .html.
What would be the best approach so I don't have to manually edit every single html file so all the links have .html at the end?
froinds is offline   Reply With Quote
Old 02-14-2005, 03:44 PM   #2
05ign
Prospect
 
Join Date: Feb 2005
Location: San Diego
Posts: 7
I Think

I believe you can change your httpd.conf file to read both of them, by adding the index.htm instead of changing it.
05ign is offline   Reply With Quote
Old 02-14-2005, 04:05 PM   #3
froinds
Triple-A Player
 
Join Date: May 2004
Posts: 158
Like this?
# The index.html.var file (a type-map) is used to deliver content-
# negotiated documents. The MultiViews Option can be used for the
# same purpose, but it is much slower.
#
DirectoryIndex index.html index.html.var index.htm index.htm.var
froinds is offline   Reply With Quote
Old 02-14-2005, 04:26 PM   #4
05ign
Prospect
 
Join Date: Feb 2005
Location: San Diego
Posts: 7
Hmm

Mine looks like this:

# DirectoryIndex: Name of the file or files to use as a pre-written HTML
# directory index. Separate multiple entries with spaces.
#
<IfModule mod_dir.c>
DirectoryIndex index.html index.htm
</IfModule>
05ign is offline   Reply With Quote
Old 02-14-2005, 04:39 PM   #5
mark hunte
MVP
 
Join Date: Apr 2004
Location: Hello London Calling
Posts: 1,787
From what I read you want to not only change the index but also all htm/html files.

I found this
save this as an executable file

Code:
#!/bin/tcsh
set fn_new={$1:r}.$2
echo renam $1 $fn_new
mv $1 $fn_new
Then run this
Code:
find ~/Desktop/webfolder -name "*.htm" -exec ./renamex {} html \;
on your directory MAKE SURE YOU SELECT THE DIRECTORY to do the find in, as I have done here (~/Desktop/html )or you may change files you do not want changing.
This will change all htm file to html in the directory and sub directories



I am assuming that you do not have spaces in file name and directories.

As from what I understand this will not like them.
This script is from
http://www.osxfaq.com/tips/unix-tric...9/wednesday.ws
mark hunte is offline   Reply With Quote
Old 02-14-2005, 11:30 PM   #6
Gnarlodious
Major Leaguer
 
Join Date: Oct 2003
Location: Santa Fe
Posts: 372
A 2 part solution...

This will be a 2 part solution.

this Applescript will make a text file on your desktop of all files with the extension you tell it to find in the folder you tell it to look in:

Code:
property fileExt : ".html"

tell application "Finder"
	set inFolder to choose folder with prompt "Select a folder:" default location (home as string) & "Sites" as alias
	display dialog "What kind of file:" default answer fileExt
	set fileExt to the text returned of the result
end tell

do shell script "find " & POSIX path of inFolder & " -type f -name '*" & fileExt & "' > ~/Desktop/FilesProcessed.txt"
My next post will be the sed solution you mentioned to change all occurrences of someString to newString.

Last edited by Gnarlodious; 02-15-2005 at 01:55 AM.
Gnarlodious is offline   Reply With Quote
Old 02-15-2005, 02:00 AM   #7
Gnarlodious
Major Leaguer
 
Join Date: Oct 2003
Location: Santa Fe
Posts: 372
this will take your list of files and do the sed replacement on every file listed. but watch out, it will also change links to other domains so it will need some postrun checking.

These 2 scripts can easily be combined into one but for the beginner it makes it easier to understand what's going on with separate scripts.

Code:
tell application "Finder" to set someFile to file "FilesProcessed.txt" of desktop as alias

open for access someFile

read someFile
set fileList to paragraphs of the result
close access someFile

repeat with someFile from 1 to count of fileList
	set thisFile to item someFile of fileList
	if thisFile is not "" then
		do shell script "tr -s '\\r' '\\n' < " & thisFile & " > /private/tmp/tempFile"
		do shell script "sed 's|\\.htm\"|\\.html\"|g' /private/tmp/tempFile > " & thisFile
	end if
end repeat
-- Gnarlie's Applescript page:
http://Gnarlodious.com/Apple/AppleScript/
Gnarlodious is offline   Reply With Quote
Old 02-20-2005, 08:42 AM   #8
weltonch777
Triple-A Player
 
Join Date: Feb 2005
Location: Bellevue, WA
Posts: 233
or you can run this in the affected directories....

Code:
#/bin/bash
#appends the letter l to the end of all filenames ending in .htm

for filename in *htm
do
mv "filename" "$filename"l
done

#replaces all .htm with .html inside the files

for filename in *html
do 
cat "$filename" | sed 's/.html/.htm/g' | 
sed 's/.htm/.html/g' > "$filename".tmp
mv "$filename".tmp "$filename"
done
This way accepts spaces and most "odd" characters in filenames.
sed turns all html's to htm's to begin with, so you don't end up with htmll's
at the end. You may have to "tweak" the sed replacements to ensure that you only change hrefs that point to your pages. (instead of ones pointing to outside pages.)

Unfortunately, that is not always as easy as it sounds.....

Chris.

Last edited by weltonch777; 02-20-2005 at 09:12 AM.
weltonch777 is offline   Reply With Quote
Old 02-20-2005, 10:13 AM   #9
froinds
Triple-A Player
 
Join Date: May 2004
Posts: 158
thanks a lot for all the help guys
froinds is offline   Reply With Quote
Old 02-20-2005, 10:56 AM   #10
guardian34
All Star
 
Join Date: Jul 2004
Posts: 713
Quote:
Code:
cat "$filename" | sed 's/.html/.htm/g' | 
sed 's/.htm/.html/g' > "$filename".tmp
mv "$filename".tmp "$filename"

You could also use perl for that part:
Code:
perl -i -p -e 's/.html/.htm/g; s/.htm/.html/g;' $filename
guardian34 is offline   Reply With Quote
Old 02-20-2005, 11:01 AM   #11
weltonch777
Triple-A Player
 
Join Date: Feb 2005
Location: Bellevue, WA
Posts: 233
Quote:
Originally Posted by guardian34
You could also use perl for that part:
Code:
perl -i -p -e 's/.html/.htm/g; s/.htm/.html/g;' $filename

Very nice.. I shall remember that

Chris
weltonch777 is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



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


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.