批量替换文本文件内的文本(Linux / OSX命令行) [英] Batch replace text inside text file (Linux/OSX Commandline)

查看:135
本文介绍了批量替换文本文件内的文本(Linux / OSX命令行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,我要替换http://的每个实例。与rtmp://。



这些文件具有.txt扩展名,分布在多个文件夹和子文件夹中。



我基本上是在寻找一种方式/脚本,通过每个文件夹/子文件夹和每个文件,如果它发现里面的http替换为rtmp。



提前感谢。

解决方案

c $ c>查找和 sed

  find。 -type f -name \ * .txt -exec sed -i.bak's | http:// | rtmp:// | g'{} + 

这将创建每个文件的备份。我建议你检查几个,以确保它做了你想要的,然后你可以使用

 查找删除它们。 -name \ * .bak -delete 


I have hundreds of files where I need to change a portion of its text.

For example, I want to replace every instance of "http://" with "rtmp://" .

The files have the .txt extention and are spread across several folders and subfolder.

I basically am looking for a way/script that goes trough every single folder/subfolder and every single file and if it finds inside that file the occurrence of "http" to replace it with "rtmp".

Thanks in advance.

解决方案

You can do this with a combination of find and sed:

find . -type f -name \*.txt -exec sed -i.bak 's|http://|rtmp://|g' {} +

This will create backups of each file. I suggest you check a few to make sure it did what you want, then you can delete them using

find . -name \*.bak -delete

这篇关于批量替换文本文件内的文本(Linux / OSX命令行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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