----- Original Message -----
From: "Daniel Ajoy" <dajoy@...>
To: <geeksthatgawk@yahoogroups.com>
Sent: Wednesday, December 11, 2002 7:07 PM
Subject: [Geeks that Gawk] msplit
> This is the msplit script that splits a unix mailbox file into
> many separate files. Could someone please help me translating
> it to gawk?
>
> Daniel
>
>
>
> msplit.pl
>
> #!/usr/bin/perl
>
> #$fname = $ARGV[0];
> #$num = $ARGV[1];
> #$prefix = $ARGV[2];
> #$suffix = $ARGV[3];
>
> $fname = shift;
> $num = shift;
> $prefix = shift;
> $suffix = shift;
>
> $nmsgs = 0;
> $filenum = 0;
> $ofile = $prefix . sprintf("%04d",$filenum) . $suffix;
> open(OFILE,">" . $ofile);
> open(INFILE,"<" . $fname);
> while (<INFILE>) {
> if(/^From /){
> $nmsgs = $nmsgs + 1;
> if ($nmsgs > $num) {
> $filenum = $filenum + 1;
> $ofile = $prefix . sprintf("%04d",$filenum) . $suffix;
> close OFILE;
> open(OFILE,">" . $ofile);
> $nmsgs = 0;
> }
> }
> print OFILE $_;
> }
> close OFILE;
> close INFILE;
>
Literal translation follows (untested):
#!/bin/awk
# msplit.awk
BEGIN {
num = ARGV[2]
prefix = ARGV[3]
suffix = ARGV[4]
delete(ARGV[4])
delete(ARGV[3])
delete(ARGV[2])
ofile = prefix sprintf("%04d",filenum) suffix
while (getline) {
if (/^From /){
nmsgs ++
if (nmsgs > num) {
filenum++
ofile = prefix sprintf("%04d",filenum) suffix
close(ofile)
nmsgs = 0
}
}
print $0 > ofile
}
close(ofile)
}
Run as:
$ msplit.awk filename num prefix suffix
HTH
Peter S Tillier
__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com