![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Aug 2007
Location: online at woocha.com
Posts: 24
Rep Power: 0
![]() |
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 ' $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? |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
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. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
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 ; doneIf 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. |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Nov 2007
Posts: 86
Rep Power: 1
![]() |
Re: Alter many files, all with same variable in them
i use TextMate
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Aug 2007
Location: online at woocha.com
Posts: 24
Rep Power: 0
![]() |
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 ![]() |
|
|
|
|
|
#6 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 762
Rep Power: 3
![]() |
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
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |