Re: Spliting file and save it with date...

"[email protected] [sed-users]" <[email protected]> 16 Sep 2017 18:05:57 +0000
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
That looks really great, very useful and creative. But it does not seem to work for some reason. I can see what you are doing and it should work. 

$ cat thephpfile # I simplified a little by removing wind and comment
<?php
$date = "2017-07-23";
$time = "23:59";
$wh_solar = "1387";
<?php
$date = "2017-07-24";
$time = "23:59";
$wh_solar = "1125";

$ sed '/^</!{H;$!d};x;1d;s/.*\n$date = "\([^"]*\).*/cat >\1.data <<EOD\n&\nEOD/' thephpfile > cmd2.sh

$ diff -s thephpfile cmd2.sh
Files thephpfile and cmd2.sh are identical

////////////////////////

So nothing happened, there was no transformation. :( 

Looking a little more, the s command is not running, because the pattern does not quite match. The multiple spaces before the equals sign do not match. I think it needs to be (add * before =):

s/.*\n$date *= "\([^"]*\).*/cat >\1.data <<EOD\n&\nEOD/

$ sed '/^</!{H;$!d};x;1d;s/.*\n$date *= "\([^"]*\).*/cat >\1.data <<EOD\n&\nEOD/' thephpfile
cat >2017-07-23.data <<EOD
<?php
$date = "2017-07-23";
$time = "23:59";
$wh_solar = "1387";
EOD
cat >2017-07-24.data <<EOD
<?php
$date = "2017-07-24";
$time = "23:59";
$wh_solar = "1125";
EOD

/////////////////////

The other little problem is that "$date" will be treated as a variable, so the script will not work as expected, "$date" will end up blank. 

$ cat 2017-07-23.data
<?php
 = "2017-07-23";
 = "23:59";
 = "1387";

I know there is some way to make the "here document" ignore the substitution, by quoting EOD, or the dollar sign could be escaped in a separate step. Again, it is an incredibly useful method you explained.

Daniel
 



[Non-text portions of this message have been removed]