sed:替换一段文字 [英] sed: replace a block of text

查看:577
本文介绍了sed:替换一段文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆文件,从一个代码块开始,我正试图用另一个替换.

I have a bunch of files, starting with a block of code and I'm trying to replace with another.

替换:

<?php
$r = session_start();
(more lines)

使用:

<?php
header("Location: index.php");
(more lines of code)

所以我试图用sed 's/<?php\n$r = session_start();/<?php\nheader...匹配该块,但是它不起作用.

So im trying to match the block with sed 's/<?php\n$r = session_start();/<?php\nheader... but it doesn't work.

在此发生的事情以及如何实现这一目标方面,我将不胜感激.我正在考虑改为使用python.

I would appreciate help in what is happening here and how to achieve this. I'm thinking in doing this with python instead.

谢谢!

推荐答案

这可能对您有用(GNU sed):

This might work for you (GNU sed):

sed -i '1i\
This is a\
new block of\
code
1,/$r = session_start();/d' file 

或者,如果您希望将新代码放在文件中:

Or if you prefer to place the new code in a file:

sed -i '1r replacement_code_file
1,/$r = session_start();/d' file

全部一行:

sed -i -e '1r replacement_code_file' -e '1,/$r = session_start();/d' file

这篇关于sed:替换一段文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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