如何用sed某一行之前加入一些内容呢?

首先上代码:
htm_file=text.html

tmp_htm$(cat $cat $htm_file)

tmp_htm=$(echo $tmp_htm | sed "/<!--otherstop---->/i\ffdsafdf")

echo $tmp_htm

而text.html的内容是:
<html>
<head>
<!--otherstart-->
<!--otherstop-->

</head>

</html>
我的本意是插入到<!--otherstop-->的上一行,也就是位于<!--otherstart-->和<!--otherstop-->之间。
但是,这句直接插入到了文件的开头,也就是<html>的上面。
我觉得,可能是tmp_htm保存htm_file的内容的时候,将回车去掉了,所以直接加在了开头。
那该如何解决呢?

sed -i '/otherstop/i\ffdsafdf' text.html

这样就可以了。


首先确保 text.html 为unix格式,如无法确认,请先执行下面这条语句:

dos2unix text.html

温馨提示:答案为网友推荐,仅供参考
相似回答