[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Splitting up a text file



> Date: Tue, 19 Mar 1996 16:00:13 -0500
> Mime-Version: 1.0
> Content-Type: text/plain; charset="us-ascii"
> From: wjdennen@COLBY.EDU (Bill Dennen)
> Sender: owner-mac-perl@iis.ee.ethz.ch
> Precedence: bulk
> 
> Hi there-
> 
> Okay... a very newbie question. Please don't flame me!
> 
> I would like to write a MacPerl script which will help do the following....
> 
>         Say I have an ASCII text file with about 40,000 or more lines. I'd
>         like to split that file into smaller files, each with 10,000
>         lines.
> ...


Here's a script which I wrote to split files on unix, both text and "binary".
You'll have to make minor modifications for macperl (i.e., the command line
args. and the #!...perl stuff).


#!/usr/gnu/bin/perl --          # -*- Perl -*-
#-----------------------------------------------------------
# splitbinfile - Split a binary file into smaller files.
#
# usage:
#   $ splitbinfile file [-N] [-V]
#   $ splitbinfile -h
#
# Where "-N" means make each output file "N" bytes (default
# is 10,000).  The output files are "file.1", "file.2", ....
# The "-V" option turns on "verbose" output.
#-----------------------------------------------------------

$usage = "usage: splitbinfile file [-size_of_each_split_file] [-V]";
$size = 10000;

while ($#ARGV >= 0)
{
    if    ($ARGV[0] =~ /^-(\d+)/)
    {
        $size = $1;
    }
    elsif ($ARGV[0] =~ /^-V/)
    {
        $VERBOSE++;
    }
    elsif ($ARGV[0] =~ /^-h/)
    {
        print "$usage\n";
        exit 0;
    }
    elsif ($ARGV[0] =~ /^-/)
    {
        print STDERR
            "Unrecognized argument \"$ARGV[0]\" ignored.\n",
            "($usage)\n";
    }
    else
    {
        $file = $ARGV[0];
    }

    shift;
}

if ($VERBOSE)
{
    print STDOUT
        "splitbinfile:\n",
        "  Output file size: $size\n",
        "  Input file:       $file\n";
}

open (IN, "<$file") ||
    die "Could not open input file \"$file\".\nStopped at";
binmode IN;                     # Ignored for Unix

$suffix  = 0;
$bytecnt = 0;

while (read (IN, $block, $size,))
{
    $suffix++;
    open (OUT, ">$file.$suffix") ||
        die "Could not open output file \"$file.$suffix\".\nStopped at";
    print OUT $block;
    close OUT;
}

-- end of file --

Hope it's useful.

dean
+-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+
|   Dean Wampler, Ph.D.                   email:  dean@amc.com        |
|   Applied Microsystems Corp.            web:    http://www.amc.com  |
|   PO Box 97002                          office: (206) 882-5394      |
|   Redmond, WA  98073-9702  USA          fax:    (206) 883-3049      |
+---------------------------------------------------------------------+
|                    "Will appear on TV for food"                     |
+-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+