开发者

Find and replace texts in multiple files

开发者 https://www.devze.com 2023-02-22 07:33 出处:网络
开发者_开发百科I migrated a typo3 website (LAMP) from a knopix server to a ubuntu 10.04 machine. Now half of the site kinda works the other have doesn\'t. I think I found the problem but can\'t resolv
开发者_开发百科

I migrated a typo3 website (LAMP) from a knopix server to a ubuntu 10.04 machine. Now half of the site kinda works the other have doesn't. I think I found the problem but can't resolve it cause i'm not mathematical enough so I can do good regex stuff.

On new server the files are in "/var/www/cms" The old server had them in /media/sda3/www/quickstart-4.2.1/

I think if all files that are in "/var/www/cms" including sub-dirs, that contain the "/media/sda1/www/quickstart-4.2.1/" string are to be replaced with "/var/www/cms" the site is going to work properly.

Help would be very much appreciated by this unix newbe. Thanks in advance!

B


Find based pipelines are your friend. This answer assumes a Linux/Unix based OS.

I highly recommend just cutting each command, one at a time, pasting onto the command line Don't try to turn this into a shell script until you have more experience. Make sure you understand what each step is doing and to triple check that the results are really what they are supposed to be and NOT just what you want it to be OR assume it should be ;-!)

# goto the root of your new filesystem
cd /var/www/cms

# print out the filenames with the old path in it
find . -print | xargs grep -l "/media/sda1/www/quickstart-4.2.1/" 
# find . -print all fileNames
              # take output from find (via pipe)
                # xargs manages creation of command line with filenames from pipe
                     # grep -l saarchs for target string and prints only file names

# make sure there are no files in this list that shouldn't be modified.

# backup files in list
find . -print | xargs -I {} /bin/cp {} {}.sav_20110405
# note that the sav_20110405 is date composed by YYYYMMDD.
# A good habit to get into

# run a test on one file from list above
 testFile=/var/www/cms/yourFileNameHere.html

find . -print -name ${testFile} \
| grep -v 'sav_20110405$' \
| while read fName ; do 
     ex - ${fName} <<EOS
     :%s/\/media\/sda1\/www\/quickstart-4.2.1/\/var\/www\/cms/g
     :wq
EOS

done

This is nice because with a sed solution, you have to manage redirection of the fix to a new file and then rename the fixed file to original file.

The ex editor is like the vi editor, but is meant for batch input of instructions AND the benefit is that you can issue the 'write' command and 'q' quit.

Important Some shells and or ex commands have problems with this sort of construct. Thats why we're testing on one file that is backed up. Make sure the file is OK and your backup file is in place before proceeding.

# inspect the 'fixed file' and be *really* sure there it is correct
# also be sure the ${testFile}.sav_20110405 did NOT get modified.

# run command for all files
find . -print  \
| grep -v 'sav_20110405$' \
| while read fName ; do 
     ex - ${fName} <<EOS
     :%s/\/media\/sda1\/www\/quickstart-4.2.1/\/var\/www\/cms/
     :wq
EOS

done

Make a note in your todo list for 1 month later, come back and delete the sav files.

find . -print -name '*.sav_20110405'

pick a filename

find . -print -name 'myTestFile.sav_20110405' | xargs /bin/rm

make sure that was the only file deleted.

Now clean up rest of files

find . -print -name '*.sav_20110405' | xargs /bin/rm

Good luck AND triple check everything ;-)!.

I hope this helps.

P.S. as you appear to be a new user, if you get an answer that helps you please remember to mark it as accepted, and/or give it a + (or -) as a useful answer.


Doesn't look like you need regex for this.
It's a normal string search/replace that you can do with any text editor. Many text editors can also not just search/replace one opened file but also search a hole directory. I often use jEdit

Also it should be much easier not to edit the files directly on the server but to work with a local copy and transfer the files once they are fixed.

0

精彩评论

暂无评论...
验证码 换一张
取 消