There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer black screen blue screen boot bsod computer connection crash css dell display driver drivers email error excel explorer firefox firefox 3 game hard drive internet internet explorer itunes laptop linux malware network networking outlook outlook 2003 outlook express partition password printer problem ram router slow software sound sprtcmd.exe trojan usb video virus vista windows windows xp wireless
Web Design & Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
script to change unix path to windows path in all files


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
csross's Avatar
Member with 93 posts.
 
Join Date: Sep 2004
Experience: Intermediate
28-Apr-2006, 11:55 AM #1
script to change unix path to windows path in all files
I need a shell script to recursively read through a directory and change all instances in a program of /var/www/html/ to /www/Apache2/htdocs/. Additionally I want it to print the full path and name of the file it changed. I can use bash or csh or even perl.

Thank you
lotuseclat79's Avatar
Distinguished Member with 10,521 posts.
 
Join Date: Sep 2003
Location: -71.45091, 42.27841
29-Apr-2006, 08:01 PM #2
Hi csross,

Please confirm the following information:
1) You are writing a script on a unix platform - (Y/N)?
2) The files you are dealing with are text files, and you want to edit a hard-coded path in the files with a text modifcation where the from string is "/var/www/html/" and the to string is "/wwwApache2/htdocs/". (Y/N)?

If the answer to both questions is Y or Yes, then its time you learned the find command in unix. Consult the man page before you code the script in whatever language you choose. Here is an example:

Let's assume we are located at the folder /src for the purpose of illustration which has subfolders with .c source files in them that are the targets of the modifications you want to make. So, for example in a C shell script which you develop:

#/bin/csh
cd /src
find . -name "*.c" -exec sed s/\/var\/www\/html\//\/ww\/Apache2\/htdocs\//g -print
end
exit

Note: the format for the sed command is s/pattern/replacement/flag, and the pattern in this case is \/var\/www\/html\/, where the '/' characters need to be escaped in the shell to get the equivalent: /var/www/html/ to be the pattern; similarly the replacement is: \/ww\/Apache2\/htdocs\/ so that the equivalent is: /www/Apache2/htdocs/

The find command work like this: being position at /src directory, the '.' implies that the find command will search from the current directory and all of its subdirectories - thus meeting your recursive decent requirement. It will the search for all files that are ".c" files that use the .c extension, i.e. C source files in this example. The -exec modifies what the find command is instructed to do when it finds a .c file, i.e. execute the sed command on the .c file replaceing all instances of the pattern with the replacement strings globally on each line - and note: the sed command is by default globally instructed to perform the replacement, so that every instance of the patttern string will be replaced by the replacement string. The print then satisfies your requirement for printing the pathnames for the *.c files found.

This is all from memory, and its been a while since I have coded in C, so play with the sed and find commands on a test experiment directory with .c files before you do it on the real deal files and directories to assure youself that it is working as you want it to work.

-- Tom
__________________
The independence created by philosophical insight is - in my opinion - the mark of distinction between a mere artisan or specialist and a real seeker after truth. - Einstein wrote in 1944.

Some say knowledge is power, I say knowledge without action is powerless. - lotuseclat79

Don't confuse action with movement. - Hemingway to Gardner

Imagination is more important than knowledge. - Einstein
lotuseclat79's Avatar
Distinguished Member with 10,521 posts.
 
Join Date: Sep 2003
Location: -71.45091, 42.27841
01-May-2006, 08:30 AM #3
The more I thought about it, I think I left out '{}' from the find command in my previous message. I think - without trying it - that it should have been:

find . -name "*.c" -exec sed s/\/var\/www\/html\//\/ww\/Apache2\/htdocs\//g {} \; -print

or something like that.

The {} substitutes the file .c that was found for the sed command, and the \; delimits the sed command.

-- Tom
__________________
The independence created by philosophical insight is - in my opinion - the mark of distinction between a mere artisan or specialist and a real seeker after truth. - Einstein wrote in 1944.

Some say knowledge is power, I say knowledge without action is powerless. - lotuseclat79

Don't confuse action with movement. - Hemingway to Gardner

Imagination is more important than knowledge. - Einstein
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 12:45 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.