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

"Jim Hill [email protected] [sed-users]" <[email protected]> Sat, 16 Sep 2017 06:04:49 -0700
Newsgroups gmane.editors.sed.user
Message-ID <CAEE75_0P3MiCnwjd_DdnfDvDfqPX4i=RngsEbKYOaHQ3CgP4Cw@mail.gmail.com>
This is one of those tasks sed turns into an easy oneliner.

The idiom I use for multiline gaggles with identifiable start lines is
 `/start/!{H;$!d};x;1d`.  sed scripts only continue past that with a full
gaggle in the pattern buffer.  H is "append this line to the hold buffer
with a leading newline", x is "exchange this line with the hold buffer", d
is best pronounced here "drop this line for now".  So on the first line,
you'll have the start-line pattern, it will be exchanged with the hold
buffer and then `1d` since it's the first line dropped.  Subsequent lines
that don't match the start pattern will be `H` appended to the hold buffer
with a leading newline and `$!d` dropped if not eof. Subsequent start lines
(and the eof line) will be exchanged with the accumulated gaggle in the
hold buffer, leaving the line buffer with the accumulated gaggle where you
can have your way with it.

So for this,

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

will transform each gaggle into a command to dump its contents into a file
named for its date field, and if you remove the `#` will pipe the results
through the shell to actually do it.


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