Unix Shell循环遍历文件并替换文本 [英] Unix Shell Loop through files and replace texts

查看:647
本文介绍了Unix Shell循环遍历文件并替换文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这些东西还很陌生,我需要一个Shell文件来循环遍历文件夹中的所有".xml"文件,并进行一些文本替换. 到目前为止,我已经提出了这个建议:

I am fairly new to this stuff, and I need a shell file to loop through all ".xml" files in a folder, and do some text replacements. So far I have come up with this:

sed "s/old_text/new_text/g" testfile.xml -i

但是,我希望它能在当前文件夹中的所有xml文件上运行,而不仅仅是在"testfile.xml"上运行.此外,如何备份原始文件?

However, I want this to run on all xml files in the current folder, not just on "testfile.xml". Furthermore, how can I make a backup of the original file ?

欢迎任何输入! 非常感谢!

Any input is more than welcomed! Thankls a lot!

推荐答案

要在所有xml文件上运行sed,只需指定通配符

To run sed on all the xml files, just specify the wildcard

sed "s/old_text/new_text/g" *.xml -i

要创建备份,只需在-i之后指定扩展名:

To create a backup, just specify the extension after -i:

sed "s/old_text/new_text/g" *.xml -i~

请注意,通常最好使用XML感知工具来处理XML.

Note that's usually better to use XML aware tools to handle XML.

这篇关于Unix Shell循环遍历文件并替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆