搜索并替换linux中所有文件中的代码块 [英] search and replace block of code in all files in linux

查看:126
本文介绍了搜索并替换linux中所有文件中的代码块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从本质上讲,这个问题与在这里一次又一次出现的问题相似:

This question is philosophically similar to a question that's come up time and time again here:

  • Search and replace text in all files of a linux directory
  • How to search-replace content within files in Ubuntu Linux?
  • Linux search and replace a complex string

但是它们(除了最后一个例外)都处理简单的替换.我有一个很大的代码块出现在许多文件中(我希望复制/粘贴源代码应受到法律的惩罚),我需要替换它.

But they (with the exception of the last one) all deal with simple replacements. I have a somewhat large block of code which appears in many files (I wish copy/pasting source code was punishable by law), and I need to replace it.

有什么可以帮助我的吗?我想避免将块"sed"化(就像上面的第三个类似问题一样),因为这将花费一天的大部分时间.如果我必须临时安装某些特定的文本编辑器,那就这样吧.

Is there anything that will help me out here? I want to avoid "sed"ifying the block (as is done in the third similar question above) because that will take the better part of a day. If I have to temporarily install some particular text editor, so be it.

推荐答案

这个问题的好处是您正在处理固定字符串而不是正则表达式...这使问题相对简单.您可以写点东西为您完成这项工作,而不会出现太多问题.

The nice thing about your question is that you're dealing with fixed strings rather than regular expressions...which makes the problem relatively simple. You could write something to do the job for you without too many problems.

我链接到的解决方案不是最佳解决方案,但是它确实有效,并且它不仅仅依赖于开头/结尾行的匹配(也就是说,它仅替换逐字匹配).基本思想是:

The solution I've linked to isn't optimal, but it does work, and it doesn't rely on matching only opening/closing lines (that is, it only replaces verbatim matches). The basic idea is:

  • 读入定义我们要查找的文件
  • 读取定义我们替换项的文件
  • 遍历所有其他参数,查找搜索字符串并将其替换为替换文本

这篇关于搜索并替换linux中所有文件中的代码块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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