CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
split file using ascii code 12

 



boobz
New User

Mar 26, 2008, 5:28 AM

Post #1 of 4 (3319 views)
split file using ascii code 12 Can't Post

Hi,

I have a file which is the concatenation of multiple emails files (exported from Lotus Notes), each file being separated by a ascii code 12. I would like to split this file into smaller files using this code.

Here is what I have done until now :


Code
my $chr = chr(12); 

my $prefix = 'slice';
$count = 0;

open (FILE, $ARGV[0]);
while (<FILE>) {
if (/$chr/) {
$count++;
open (OUTPUT,">$prefix.$count");
print OUTPUT $`; # want what's before
close OUTPUT;
}
}


I get slice files but they're empty..

Help would be nice.
Regards


KevinR
Veteran


Mar 26, 2008, 9:27 AM

Post #2 of 4 (3317 views)
Re: [boobz] split file using ascii code 12 [In reply to] Can't Post

When I print the value of this line:

my $chr = chr(12);

I get a dot '.';

Why not check for the dot in the regexp instead of $chr?

if (/\./) {
-------------------------------------------------


(This post was edited by KevinR on Mar 26, 2008, 9:31 AM)


boobz
New User

Mar 27, 2008, 1:18 AM

Post #3 of 4 (3306 views)
Re: [KevinR] split file using ascii code 12 [In reply to] Can't Post

Hi,

When I try to print chr(12), nothing appears, I only get the prompted shifted up as if I typed the unix 'clear' command.


Quote
Why not check for the dot in the regexp instead of $chr?


Because all I'm sure in this file is the separation caracter which is ascii 12 (that is chr(12)).

Thanks for any other suggestions.


boobz
New User

Mar 27, 2008, 6:59 AM

Post #4 of 4 (3295 views)
[SOLVED] split file using ascii code 12 [In reply to] Can't Post

For the records, here is the solution :


Code
#!/usr/bin/perl 
#
$count = 0;
my $filename = 'input.txt';
open my $infh, '<', $filename or die("$!\n");
{
local $/ = chr(12);
while (my $block = <$infh>) {
chomp;
$count += 1;
open my $outfh, '>', "slice$count.txt" or die "$!\n";
print $outfh $block;
close $outfh;
}
}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives