2

我正在尝试使用 sed 在字符串匹配后插入一些文本,但我无法在附加文本中使用前导回车符。我有 ...

sed -i "/^#Comment to append text after/a \n[$username.conn]\nipAddress=$ipAddr\nportNumber=$portNum" file

我想在那个 3 行插入之前有一个回车,所以输出是......

#Comment to append text after

[$username.conn]
ipAddress=xxx.xxx.xxx.xxx
portNum=yyyy

但是将 a\n放在 just 前面会[$username.conn]导致前导 n...

#Comment to append text after
n[$username.conn]
ipAddress=xxx.xxx.xxx.xxx
portNum=yyyy

关于如何做到这一点的任何建议?

4

2 回答 2

2

我认为问题是由于对反斜杠的特殊处理sed

sed "/^#Comment to append text after/a\\\n[$username.conn]\nipAddress=$ipAddr\nportNumber=$portNum" input

或更清楚地说:

sed '/^#Comment to append text after/a \
\
['$username'.conn]\
ipAddress='$ipAddr'\
portNumber='"$portNum" input
于 2013-04-17T16:46:39.657 回答
0

我不知道为什么,但使用\\n\n只是第一次)对我有用。

像这样:

sed -i "/^#Comment to append text after /a \\n [$username.conn]\nipAddress=$ipAddr\nportNumber=$portNum" /usr/Jas/conf/drp.ini

我在 gnu 的 sed commnad 上试过这个。

于 2013-04-17T16:45:19.673 回答