ilium007
ilium007

Reputation: 589

sed append line - how do I get a new line?

I have spent hours on this but can't crack it. I am using sed on OSX .

This is the code:

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

And the output that I get which is wrong is (see the first line):

$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',
);

It should be on a new line like below:

$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',
);

Any help would be greatly appreciated !

Upvotes: 1

Views: 268

Answers (2)

Scrutinizer
Scrutinizer

Reputation: 9936

\n is not supported as a newline character in the replacement part of the substitute command of regular sed (only in GNU sed).

For example to prepend a newline to a pattern, instead of

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

use

sed 's/pattern/\
&' file

The \ should be the last character on the line.

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 882606

Don't use substitute for this, sed has a perfectly good append command for adding a line after the current one, without fiddling around with newline or storage of regex results:

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'
);

The machinations with the quotes are to allow you to put literal single quotes within the command.

Alternatively, you can use double quotes on the outside, you just have to be careful that the shell doesn't interpret your dollar-variables:

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'
);

There's also an opposing insert command i for inserting before the current line but it's append you want in this case.


And, if you're having trouble with mixing quote types (perhaps due to an older bash under OSX), you can put the sed commands into a file and use sed -f to run them:

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'
);

That gets around any quoting battles between the shell and sed. If that still doesn't work, see this link, which suggests installing GNU sed instead.

Upvotes: 1

Related Questions