1

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 !

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

12 Comments

I did try 'append' but could not get the single quotes to escape so they would show in the output.
I keep getting this error "sed: 1: "/^\$dokuwiki_hash = /a\ ...": extra characters after \ at the end of a command"
So I am now running: GNU bash, version 4.2.45(2)-release (i386-apple-darwin12.2.0) And I am still getting: macmini:dokuwiki ilium007$ sed -i .bak '/^\(\$dokuwiki_hash.*\)/a\ blah '"'"'blah'"'"' blah' /Volumes/HDD2/Sites/clients/test/install.php sed: 1: "/^\(\$dokuwiki_hash.*\) ...": extra characters after \ at the end of a command
I have just installed GNU sed via homebrew and will see how I go. gsed (GNU sed) 4.2.2
This worked: /usr/local/bin/gsed -i -e "/^\$dokuwiki_hash =/a \blah" /Volumes/HDD2/Sites/clients/go2humanperformance/install.php
|
1

\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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.