Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 17th, 2008, 8:59 AM   #1
woocha
Newbie
 
woocha's Avatar
 
Join Date: Aug 2007
Location: online at woocha.com
Posts: 24
Rep Power: 0 woocha is on a distinguished road
Alter many files, all with same variable in them

I have 100's of files that access a field in a mySQL DataBase. I had to change the field name and now I have to update 100's of files. The files read:
$sql = 'SELECT user_id, username, user_colour, user_birthday FROM '
I need it to now read:
$sql = 'SELECT id, nick, user_colour, user_birthday FROM '

I know I could go through all files and execute a replace 'user_id' with 'id', but that would take forever. Does anyone know a program to do this easier?
__________________
FREE Online Auctions at:
http://www.woocha.com/auctions
woocha is offline   Reply With Quote
Old Jan 17th, 2008, 10:17 AM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,868
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Alter many files, all with same variable in them

LOL!

Folks. A perfect example of why you use common code and constants.

I know it's too late now woocha, but for future reference: if you want to perform the same sql query in multiple places, wrap it in a common function. Similarily, for databases, table names (and maybe even column names), assign them to constant variables.

That would make this process so much easier.

All I can suggest now is that you could write a simple script in a language such as Python or PHP, with a primary function to search and replace the contents of every file in a directory. This wouldn't be so hard to whip up quickly.

P.S. I don't think you should have 100 PHP files... Either you're exaggerating, or you've done something very wrong. The only projects I've seen like that are large-scale reputable open-source web applications such as Wordpress and Xoops. And even then, it still may not even be as many as 100. If the project you're working on is really that large, I'm surprised you haven't figured out the convention of common code and constants yet.
Sane is offline   Reply With Quote
Old Jan 17th, 2008, 11:07 AM   #3
ZenMasterJG
Hobbyist Programmer
 
ZenMasterJG's Avatar
 
Join Date: Nov 2004
Location: Boston, MA
Posts: 148
Rep Power: 4 ZenMasterJG is on a distinguished road
Send a message via AIM to ZenMasterJG
Re: Alter many files, all with same variable in them

If you're on *nix and you want to replace EVERY INSTANCE of user_id with id in EVERY FILE IN A DIRECTORY, do the following:
1) cd to the directory you want to do the global replace in.
2) execute this command:
for file in `ls`; do mv $file $file'.OLD' ; nawk ' { gsub("user_id", "id", $0) ; print $0 } ' $file'.OLD' > $file ; done
check the files are now correct. if they're not, no worries, the old versions just have .OLD at the end of them now. If the replace worked ok, just delete all the *.OLD files.

If you need to replace just certain instances of user_id, i can help you narrow down that awk business a bit. I was just being lazy and its been a damn long time since I've played with awk

Last edited by ZenMasterJG; Jan 17th, 2008 at 11:18 AM.
ZenMasterJG is offline   Reply With Quote
Old Jan 17th, 2008, 11:09 AM   #4
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 1 mbd is on a distinguished road
Re: Alter many files, all with same variable in them

i use TextMate
mbd is offline   Reply With Quote
Old Jan 17th, 2008, 1:20 PM   #5
woocha
Newbie
 
woocha's Avatar
 
Join Date: Aug 2007
Location: online at woocha.com
Posts: 24
Rep Power: 0 woocha is on a distinguished road
Re: Alter many files, all with same variable in them

Thanks for the inut guys. I acquired custome photohosting software and am trying to combine phpbb3 with phpauction3 and my photohosting software. I might have exaggerated 100's file, but it is probably pretty darn close.
thanks to all
__________________
FREE Online Auctions at:
http://www.woocha.com/auctions
woocha is offline   Reply With Quote
Old Jan 17th, 2008, 10:26 PM   #6
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 762
Rep Power: 3 Jimbo is on a distinguished road
Re: Alter many files, all with same variable in them

You could also do it with sed, again assuming a *nix system or gnu tools installed. I'm rusty, but something along the lines of:
 for file in `ls`; 
do 
    sed -i "s/SELECT user_id, username,/SELECT id, nick,/g" $file; 
done
If you want a backup copy, use -i.bkup
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I read .mbm (Multi BitMap) files? moondog Other Programming Languages 19 Aug 16th, 2007 8:59 PM
variable problem robert_sun C 1 Apr 12th, 2005 2:10 PM
Checking source codes of image, audio and video files on_auc C++ 3 Feb 21st, 2005 8:36 PM
shutil.copy corrupting files? TimL Python 0 Feb 20th, 2005 3:32 PM
Bash script to access all files in a directory shinni Bash / Shell Scripting 4 Feb 18th, 2005 3:26 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:02 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC