| Newsgroups |
gmane.editors.sed.user |
| Message-ID |
<[email protected]> |
"But second, and this is critical, you need to be absolutely totally sure that the marker "EOD" does not appear in the source data. If it does appear then (a) the data will terminate earlier (just annoying) but (b) critically, the following data will be fed to your shell as commands."
Respectfully, I do not think that is the case. I would like to see that (data terminate early, following data fed to shell as commands) in an example. To my understanding, there is no risk, because the EOD marker has to appear on a line by itself. The bash reference manual says "This type of redirection instructs the shell to read input from the current source until a line containing only word (with no trailing blanks) is seen." We know the format of the source data. It will *not* have a line solely with EOD.
Here is example with EOD in the data, works fine, data does not terminate early:
$ cat thephpfile
<?php
$date = "2017-07-23";
$time = "23:59";
$dummy = "EOD";
<?php
$date = "2017-07-24";
$time = "23:59";
$dummy = "1125";
$ sed '/^</!{H;$!d};x;1d;s/.*\n$date *= "\([^"]*\).*/cat >\1.data <<"EOD"\n&\nEOD/' thephpfile > cmd2.sh
$ cat cmd2.sh
cat >2017-07-23.data <<"EOD"
<?php
$date = "2017-07-23";
$time = "23:59";
$dummy = "EOD";
EOD
cat >2017-07-24.data <<"EOD"
<?php
$date = "2017-07-24";
$time = "23:59";
$dummy = "1125";
EOD
$ chmod 755 cmd2.sh; ./cmd2.sh
$ cat 2017-07-23.data
<?php
$date = "2017-07-23";
$time = "23:59";
$dummy = "EOD";
$ cat 2017-07-24.data
<?php
$date = "2017-07-24";
$time = "23:59";
$dummy = "1125";
So unless I am missing something, or some other fault turns up (always possible), I think Jim's solution is pretty much ideal.
Daniel
PS - To my thinking, not in "sed one-liners" because 1) maybe a bit too long, 2) maybe too special-purpose, 3) maybe nobody thought of it before.
[Non-text portions of this message have been removed]