sed追加行 - 如何获得新行?

时间:2013-03-17 01:49:00

标签: bash sed

我花了好几个小时但却无法破解它。我在OSX上使用sed。

这是代码:

sed -i.bak "s/^\(\$dokuwiki_hash.*\)$/\1\n    '"$date"'   => '"$hash"',/" install.php

我得到的错误输出是(见第一行):

$dokuwiki_hash = array(n    '2013-03-17'   => '7b62b75245f57f122d3e0f8ed7989623',
    '2005-09-22'   => 'e33223e957b0b0a130d0520db08f8fb7',
    '2006-03-05'   => '51295727f79ab9af309a2fd9e0b61acc',                                                                                                       
    '2006-03-09'   => '51295727f79ab9af309a2fd9e0b61acc',
    '2006-11-06'   => 'b3a8af76845977c2000d85d6990dd72b',
    '2007-05-24'   => 'd80f2740c84c4a6a791fd3c7a353536f',
    '2007-06-26'   => 'b3ca19c7a654823144119980be73cd77',
    '2008-05-04'   => '1e5c42eac3219d9e21927c39e3240aad',
    '2009-02-14'   => 'ec8c04210732a14fdfce0f7f6eead865',
    '2009-12-25'   => '993c4b2b385643efe5abf8e7010e11f4',
    '2010-11-07'   => '7921d48195f4db21b8ead6d9bea801b8',
    '2011-05-25'   => '4241865472edb6fa14a1227721008072',
    '2011-11-10'   => 'b46ff19a7587966ac4df61cbab1b8b31',
    '2012-01-25'   => '72c083c73608fc43c586901fd5dabb74',
    '2012-09-10'   => 'eb0b3fc90056fbc12bac6f49f7764df3',
    '2013-04-06'   => '7b62b75245f57f122d3e0f8ed7989623',
);

它应该在如下所示的新行上:

$dokuwiki_hash = array(
    '2013-03-17'   => '7b62b75245f57f122d3e0f8ed7989623',
    '2005-09-22'   => 'e33223e957b0b0a130d0520db08f8fb7',
    '2006-03-05'   => '51295727f79ab9af309a2fd9e0b61acc',                                                                                                       
    '2006-03-09'   => '51295727f79ab9af309a2fd9e0b61acc',
    '2006-11-06'   => 'b3a8af76845977c2000d85d6990dd72b',
    '2007-05-24'   => 'd80f2740c84c4a6a791fd3c7a353536f',
    '2007-06-26'   => 'b3ca19c7a654823144119980be73cd77',
    '2008-05-04'   => '1e5c42eac3219d9e21927c39e3240aad',
    '2009-02-14'   => 'ec8c04210732a14fdfce0f7f6eead865',
    '2009-12-25'   => '993c4b2b385643efe5abf8e7010e11f4',
    '2010-11-07'   => '7921d48195f4db21b8ead6d9bea801b8',
    '2011-05-25'   => '4241865472edb6fa14a1227721008072',
    '2011-11-10'   => 'b46ff19a7587966ac4df61cbab1b8b31',
    '2012-01-25'   => '72c083c73608fc43c586901fd5dabb74',
    '2012-09-10'   => 'eb0b3fc90056fbc12bac6f49f7764df3',
    '2013-04-06'   => '7b62b75245f57f122d3e0f8ed7989623',
);

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

不要使用替代品,sed有一个非常好的附加命令,用于在当前的行之后添加一行,而不用摆弄新行或正则表达式结果的存储:

pax> echo '$dokuwiki_hash = array(
    '"'"'2013-03-17'"'"'   => '"'"'7b62b75245f57f122d3e0f8ed7989623'"'"'
);' | sed '/^\$dokuwiki_hash = /a\    blah '"'"'blah'"'"' blah'
$dokuwiki_hash = array(
    blah 'blah' blah
    '2013-03-17'   => '7b62b75245f57f122d3e0f8ed7989623'
);

带引号的阴谋部分允许您在命令中放置文字单引号。

或者,您可以在外部使用双引号,您只需要小心shell不会解释您的美元变量:

pax> echo "\$dokuwiki_hash = array(
    '2013-03-17'   => '7b62b75245f57f122d3e0f8ed7989623'
);" | sed "/^\$dokuwiki_hash = /a\    blah 'blah' blah"

$dokuwiki_hash = array(
    blah 'blah' blah
    '2013-03-17'   => '7b62b75245f57f122d3e0f8ed7989623'
);

还有一个相反的插入命令i用于在当前行之前插入,但在这种情况下它会附加你想要的。


而且,如果您在混合引用类型时遇到问题(可能是因为OSX下的bash较旧),您可以将sed命令放入文件并使用sed -f运行它们:

pax> cat qq.sed

/^$dokuwiki_hash = /a\    blah 'blah' blah

pax> echo '$dokuwiki_hash = array(
    '"'"'2013-03-17'"'"'   => '"'"'7b62b75245f57f122d3e0f8ed7989623'"'"'
);' | sed -f qq.sed

$dokuwiki_hash = array(
    blah 'blah' blah
    '2013-03-17'   => '7b62b75245f57f122d3e0f8ed7989623'
);

可以解决shell与sed之间的所有引用战斗。如果不起作用,请参阅this link,建议安装GNU sed。

答案 1 :(得分:1)

\n不支持常规sed的替换命令的替换部分中的换行符(仅在GNU sed中)。

例如,为模式添加换行符,而不是

sed 's/pattern/\n&/' file

使用

sed 's/pattern/\
&' file

\应该是该行的最后一个字符。