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: mooring@grimoire.tymnet.com (Ed Mooring) Newsgroups: comp.lang.perl Subject: Re: Is there a better way to do this? Message-ID: <3952@tymix.Tymnet.COM> Date: 3 Jun 93 20:04:12 GMT References: <1993Jun1.001323.8650@dragon.acadiau.ca> Sender: usenet@tymix.Tymnet.COM Organization: BT Tymnet Bit Bucket Brigade Lines: 15 Nntp-Posting-Host: grimoire In article <1993Jun1.001323.8650@dragon.acadiau.ca> peter@dragon.acadiau.ca (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 (mooring@tymix.tymnet.com 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: merlyn@ora.com (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: References: <1993Jun1.001323.8650@dragon.acadiau.ca> NNTP-Posting-Host: kandinsky.intel.com In-reply-to: peter@dragon.acadiau.ca's message of Tue, 1 Jun 1993 00:13:23 GMT >>>>> In article <1993Jun1.001323.8650@dragon.acadiau.ca>, peter@dragon.acadiau.ca (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 $_; }' Sender: news@wdl.loral.com Organization: Loral Western Development Labs References: <1993Jun1.001323.8650@dragon.acadiau.ca> Date: Tue, 1 Jun 1993 16:52:50 GMT Lines: 32 In article <1993Jun1.001323.8650@dragon.acadiau.ca> peter@dragon.acadiau.ca (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 mab@wdl1.wdl.loral.com 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: utashiro@sran230.sra.co.jp (Kazumasa Utashiro) Subject: Re: Is there a better way to do this? References: <1993Jun1.001323.8650@dragon.acadiau.ca> <1993Jun2.115048.13743@dragon.acadiau.ca> Organization: Software Research Associates, Inc., Japan Date: Thu, 3 Jun 1993 00:49:02 GMT Message-ID: Lines: 14 In article <1993Jun2.115048.13743@dragon.acadiau.ca> peter@dragon.acadiau.ca (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