Article 3173 of comp.lang.perl:
Xref: feenix.metronet.com comp.lang.perl:3173
Path: feenix.metronet.com!news.utdallas.edu!hermes.chpc.utexas.edu!cs.utexas.edu!uunet!tymix!grimoire!mooring
From: [email protected] (Ed Mooring)
Newsgroups: comp.lang.perl
Subject: Re: Is there a better way to do this?
Message-ID: <[email protected]>
Date: 3 Jun 93 20:04:12 GMT
References: <[email protected]>
Sender: [email protected]
Organization: BT Tymnet Bit Bucket Brigade
Lines: 15
Nntp-Posting-Host: grimoire

In article <[email protected]> [email protected] (Peter Steele) writes:
>A user asked me if there was an easy way to split a file consisting
>of multiple pages separated by formfeeds into multiple files representing
>each page of the input file. For example, if the file was called "fred",
>the resulting would be "fred.1", "fred.2", etc. I came up with this:

[ code deleted ]
You could use csplit(1), I suppose.  Here's my contribution to the perl
one-liners:
       perl -014 -ne 'open(X,">$ARGV.".++$x);print X $_;close(X); $x = 0 if eof(ARGV);'
This works on multiple files or stdin. It might need a little fudging if you want to
do something special with the form-feed.

Regards,
Ed Mooring ([email protected] 408-922-7504)


Article 3099 of comp.lang.perl:
Xref: feenix.metronet.com comp.lang.perl:3099
Path: feenix.metronet.com!news.ecn.bgu.edu!wupost!howland.reston.ans.net!sol.ctr.columbia.edu!news.kei.com!ssd.intel.com!ichips!ornews.intel.com!ornews.intel.com!merlyn
From: [email protected] (Randal L. Schwartz)
Newsgroups: comp.lang.perl
Subject: Re: Is there a better way to do this?
Date: 1 Jun 93 09:37:28
Organization: Stonehenge Consulting Services; Portland, Oregon, USA
Lines: 24
Message-ID: <[email protected]>
References: <[email protected]>
NNTP-Posting-Host: kandinsky.intel.com
In-reply-to: [email protected]'s message of Tue, 1 Jun 1993 00:13:23 GMT

>>>>> In article <[email protected]>, [email protected] (Peter Steele) writes:
Peter> A user asked me if there was an easy way to split a file consisting
Peter> of multiple pages separated by formfeeds into multiple files representing
Peter> each page of the input file. For example, if the file was called "fred",
Peter> the resulting would be "fred.1", "fred.2", etc. I came up with this:
Peter> Something a little less cryptic might look like:

Peter>   perl -e '$p=1; while (<>) {/\f/ ? $p++ : `echo -n "$_" >> fred.$p`}' fred

Peter> There's probably a dozen ways to do something like this. Can anyone
Peter> come up with something "better" than either of these. Am I missing
Peter> some Unix utility that can do this sort of thing as well?

Well, if you're a sysV kinda guy, you can use "csplit".  But if you're
not V-minded, you can use Perl, pretty close to the way you did it,
but all in Perl:

perl -e '$f=1; open(F,">fred.$f"); while(<>){ $f++,open(F,">fred.$f")if/\f/; print F $_; }' <in

print "Just another Perl hacker,"
--
Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
[email protected] (semi-permanent) [email protected] (NEWSREADING ONLY)
Quote: "Welcome to Portland, Oregon, home of the California Raisins!"


Article 3097 of comp.lang.perl:
Xref: feenix.metronet.com comp.lang.perl:3097
Newsgroups: comp.lang.perl
Path: feenix.metronet.com!news.ecn.bgu.edu!wupost!math.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!agate!ames!sgi!wdl1!wdl39!mab
From: [email protected] (Mark A Biggar)
Subject: Re: Is there a better way to do this?
Message-ID: <[email protected]>
Sender: [email protected]
Organization: Loral Western Development Labs
References: <[email protected]>
Date: Tue, 1 Jun 1993 16:52:50 GMT
Lines: 32

In article <[email protected]> [email protected] (Peter Steele) writes:
>A user asked me if there was an easy way to split a file consisting
>of multiple pages separated by formfeeds into multiple files representing
>each page of the input file. For example, if the file was called "fred",
>the resulting would be "fred.1", "fred.2", etc. I came up with this:
>  rm fred.*     -- remove any existing files of this form
>  perl -ne '$p ? $p : $p++; /\f/ ?$ p++ : `echo -n "$_" >> fred.$p`' fred
>Something a little less cryptic might look like:
>  perl -e '$p=1; while (<>) {/\f/ ? $p++ : `echo -n "$_" >> fred.$p`}' fred
>There's probably a dozen ways to do something like this. Can anyone
>come up with something "better" than either of these. Am I missing
>some Unix utility that can do this sort of thing as well?

First, spawning a new process for each line in the file is really expensive,
so we want to get rid of that and do internal perl IO operations.  Second,
reading things one line at a time when we have a perfectly good seperater
in '\f' is also unecessary.  Combining these together gives the following:

perl -014 -ne 'chop if /\f$/; open(FOO,">fred.".++$p);print FOO $_;' fred

Note we no longer need to rm fred.* because we are not appending any longer.
The first option sets the input line separater to '\f'.
If you want to retain the '\f's remove the "chop if /\f$/;".  The chop needs
the conditional modifier because the last page likely doesn't end in a '\f'.

No there does not appear to be a unix tool to do what you want.

--
Perl's Maternal Uncle
Mark Biggar
[email protected]



Article 3141 of comp.lang.perl:
Xref: feenix.metronet.com comp.lang.perl:3141
Newsgroups: comp.lang.perl
Path: feenix.metronet.com!news.ecn.bgu.edu!wupost!darwin.sura.net!sgiblab!adagio.panasonic.com!chorus.mei!oskgate0.mei!wnoc-kyo!sh.wide!wnoc-tyo-news!sranha!sranhd!sran230!utashiro
From: [email protected] (Kazumasa Utashiro)
Subject: Re: Is there a better way to do this?
References: <[email protected]> <[email protected]> <[email protected]>
Organization: Software Research Associates, Inc., Japan
Date: Thu, 3 Jun 1993 00:49:02 GMT
Message-ID: <[email protected]>
Lines: 14

In article <[email protected]>
       [email protected] (Peter Steele) writes:
>> >How about:
>>
>> >    perl -014ne '`echo -n "$_" > fred.$.`' fred
>>
>> Well, this seems like the winner, as far as being the most concise. The
>> other solutions that avoid the use of echo would be more efficient.

Then how about this?

perl -014pe 'open(STDOUT,">fred.$.")' fred

--utashiro