Search the web
Sign In
New User? Sign Up
geeksthatgawk · for GNU Awk questions & discussions
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Message search is now enhanced, find messages faster. Take it for a spin.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Re: [Geeks that Gawk] msplit   Message List  
Reply | Forward Message #48 of 221 |
----- 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



Wed Dec 11, 2002 9:06 pm

peter_tillier
Offline Offline
Send Email Send Email

Forward
Message #48 of 221 |
Expand Messages Author Sort by Date

... 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...
Peter S Tillier
peter_tillier
Offline Send Email
Dec 11, 2002
9:06 pm

... You are the geek! :) It worked exactly like "msplit.pl". I made some modifications. Code follows: #!/bin/awk # msplit splits a unix mailbox file into many...
Daniel Ajoy
daniel_ajoy
Offline Send Email
Dec 12, 2002
5:21 am

... 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...
Peter S Tillier
peter_tillier
Offline Send Email
Dec 12, 2002
7:03 am
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help