查找并替换多个文件中的文本

发布于 2024-10-30 22:16:13 字数 348 浏览 4 评论 0原文

我将一个typo3网站(LAMP)从knopix服务器迁移到ubuntu 10.04机器。现在,该网站的一半可以正常工作,而另一半则不能。我想我发现了问题,但无法解决它,因为我数学不够,所以我可以做很好的正则表达式。

在新服务器上,文件位于“/var/www/cms”中 旧服务器将它们放在 /media/sda3/www/quickstart-4.2.1/

我认为如果“/var/www/cms”中的所有文件(包括子目录)包含“/media/sda1/www /quickstart-4.2.1/”字符串将替换为“/var/www/cms”,网站将正常工作。

这位 UNIX 新手将非常感谢您的帮助。 提前致谢!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感情旳空白 2024-11-06 22:16:13

基于查找的管道是您的朋友。这个答案假设基于 Linux/Unix 的操作系统。

我强烈建议一次剪切每个命令,然后粘贴到命令行上
在您有更多经验之前,不要尝试将其转换为 shell 脚本。
确保您了解每个步骤正在做什么,并三次检查结果是否确实是它们应该的结果,而不仅仅是您想要的结果或假设它应该是;-!)

# 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

这很好,因为使用 sed 解决方案,您必须管理将修复重定向到新文件,然后将修复文件重命名为原始文件。

ex 编辑器类似于 vi 编辑器,但用于批量输入指令,好处是您可以发出“write”命令和“q”退出。

重要
某些 shell 和/或 ex 命令在这种结构上存在问题。这就是我们测试一个备份文件的原因。在继续之前,请确保文件正常并且备份文件已就位。

# 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

在你的待办事项列表中记下 1 个月后,回来并删除 sav 文件。

find . -print -name '*.sav_20110405'

选择一个文件名

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

,确保这是唯一被删除的文件。

现在清理其余文件

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

祝你好运并仔细检查所有内容;-)!

我希望这有帮助。

PS,由于您似乎是新用户,如果您得到的答案对您有帮助,请记住将其标记为已接受,和/或给它 +(或 -)作为有用的答案。

\ | while read fName ; do ex - ${fName} <<EOS :%s/\/media\/sda1\/www\/quickstart-4.2.1/\/var\/www\/cms/g :wq EOS done

这很好,因为使用 sed 解决方案,您必须管理将修复重定向到新文件,然后将修复文件重命名为原始文件。

ex 编辑器类似于 vi 编辑器,但用于批量输入指令,好处是您可以发出“write”命令和“q”退出。

重要
某些 shell 和/或 ex 命令在这种结构上存在问题。这就是我们测试一个备份文件的原因。在继续之前,请确保文件正常并且备份文件已就位。


在你的待办事项列表中记下 1 个月后,回来并删除 sav 文件。


选择一个文件名


,确保这是唯一被删除的文件。

现在清理其余文件


祝你好运并仔细检查所有内容;-)!

我希望这有帮助。

PS,由于您似乎是新用户,如果您得到的答案对您有帮助,请记住将其标记为已接受,和/或给它 +(或 -)作为有用的答案。

\ | while read fName ; do ex - ${fName} <<EOS :%s/\/media\/sda1\/www\/quickstart-4.2.1/\/var\/www\/cms/ :wq EOS done

在你的待办事项列表中记下 1 个月后,回来并删除 sav 文件。

选择一个文件名

,确保这是唯一被删除的文件。

现在清理其余文件

祝你好运并仔细检查所有内容;-)!

我希望这有帮助。

PS,由于您似乎是新用户,如果您得到的答案对您有帮助,请记住将其标记为已接受,和/或给它 +(或 -)作为有用的答案。

\ | while read fName ; do ex - ${fName} <<EOS :%s/\/media\/sda1\/www\/quickstart-4.2.1/\/var\/www\/cms/g :wq EOS done

这很好,因为使用 sed 解决方案,您必须管理将修复重定向到新文件,然后将修复文件重命名为原始文件。

ex 编辑器类似于 vi 编辑器,但用于批量输入指令,好处是您可以发出“write”命令和“q”退出。

重要
某些 shell 和/或 ex 命令在这种结构上存在问题。这就是我们测试一个备份文件的原因。在继续之前,请确保文件正常并且备份文件已就位。

在你的待办事项列表中记下 1 个月后,回来并删除 sav 文件。

选择一个文件名

,确保这是唯一被删除的文件。

现在清理其余文件

祝你好运并仔细检查所有内容;-)!

我希望这有帮助。

PS,由于您似乎是新用户,如果您得到的答案对您有帮助,请记住将其标记为已接受,和/或给它 +(或 -)作为有用的答案。

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

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

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.

\ | 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.


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


pick a filename


make sure that was the only file deleted.

Now clean up rest of files


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.

\ | 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.

pick a filename

make sure that was the only file deleted.

Now clean up rest of files

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.

\ | 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.

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

pick a filename

make sure that was the only file deleted.

Now clean up rest of files

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.

倾城泪 2024-11-06 22:16:13

看起来您不需要正则表达式。
这是一个普通的字符串搜索/替换,您可以使用任何文本编辑器进行。许多文本编辑器不仅可以搜索/替换一个打开的文件,还可以搜索空目录。我经常使用 jEdit

另外,不要直接在服务器上编辑文件,而是使用本地复制并在文件修复后传输文件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文