1

我已经花了几个小时在这个上,但无法破解它。我在 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',
);

任何帮助将不胜感激 !

4

2 回答 2

1

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

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

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

利用

sed 's/pattern/\
&' file

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

于 2013-03-17T10:55:50.323 回答
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用于在当前行之前插入,但在这种情况下它是您想要的附加。


而且,如果您在混合引用类型时遇到问题(可能是由于bashOSX 下的较旧版本),您可以将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. 如果这仍然不起作用,请参阅此链接,它建议安装 GNU sed。

于 2013-03-17T01:54:30.037 回答