For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > February 2007 > Merging Files in Perl









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Merging Files in Perl
Akbar Ehsan

2007-02-01, 9:55 pm

Hello Everyone,

Is there something in Perl, a procedure, a method, a command, anything,
that would enable me to merge several tiny text files into one big text
files. I was searching on perldoc.org unsuccessfully.

Thanks.

Akbar Ehsan
Internet Services Coordinator
Ivy Tech Community College
9301 E. 59th St.
Indianapolis, IN 46216


Greg Jetter

2007-02-01, 9:55 pm

On Thursday February 1 2007 5:22 pm, Akbar Ehsan wrote:
> Hello Everyone,
>
> Is there something in Perl, a procedure, a method, a command, anything,
> that would enable me to merge several tiny text files into one big text
> files. I was searching on perldoc.org unsuccessfully.
>
> Thanks.
>
> Akbar Ehsan
> Internet Services Coordinator
> Ivy Tech Community College
> 9301 E. 59th St.
> Indianapolis, IN 46216


Sure , just open them one at a time read them in and write them back out to
the destination file one at a time , thousands of way of doing that , show us
what you got so far and we can help ya along.


Greg

Dennis McFall

2007-02-02, 3:55 am

At 08:22 PM 2/1/2007, Akbar Ehsan wrote:
>Hello Everyone,
>
>Is there something in Perl, a procedure, a method, a command, anything,
>that would enable me to merge several tiny text files into one big text
>files. I was searching on perldoc.org unsuccessfully.
>



The cat command in a *nix shell. (Can also be called by a Perl
script, if the script is necessary for some other reason):

cat file1 file2 file3 > one_big_file

or

cat file* > one_big_file

Perl:

#!/usr/bin/perl

system("cat somefile* > one_big_file") ==0 or die "cat failed: $?\n";

../dm

Akbar Ehsan

2007-02-02, 7:55 am

Here is what I have so far. A script that creates two files:
andadm.count and blmadm.count. At the end of the script I want to merge
these two files in to one file.

#!/usr/bin/perl -w

#use Net::SMTP;
use strict;
use CGI::Carp qw(fatalsToBrowser);
use CGI;

print "Content-Type: text/html; charset=3DISO-8859-1\n\n";

my $field1=3D"andadm,blmadm,coladm,echadm,elkadm";

my @array1 =3D split(/,/, $field1);

my $j =3D 0;
my $andcount =3D 0;
my $blmcount =3D 0;
#my count =3D 0;
my $FileName =3D "andadm.count";
my $FileName1 =3D "blmadm.count";

foreach (@array1) {
if ($_ eq "andadm"){
open(COUNT, "<$FileName") || die "Cannot open $FileName: $!\n";
$andcount=3D<COUNT>;
$andcount++;
close(COUNT);
open(COUNT,">$FileName");
print COUNT $andcount;
close(COUNT);
}
elsif ($_ eq "blmadm"){
open(COUNT, "<$FileName1") || die "Cannot open $FileName1: $!\n";
$blmcount=3D<COUNT>;
$blmcount++;
close(COUNT);
open(COUNT,">$FileName1\n");
print COUNT $blmcount;
close(COUNT);
}
$j++;
}


Thanks

Akbar Ehsan
Internet Services Coordinator
Central Office
Ivy Tech Community College
9301 E. 59th St.
Indianapolis, IN 46216
Phone: 1 317 921-4872

-----Original Message-----
From: Greg Jetter [mailto:greg@lazymountain.com]=20
Sent: Thursday, February 01, 2007 10:08 PM
To: beginners-cgi@perl.org
Subject: Re: Merging Files in Perl

On Thursday February 1 2007 5:22 pm, Akbar Ehsan wrote:
> Hello Everyone,
>
> Is there something in Perl, a procedure, a method, a command,

anything,
> that would enable me to merge several tiny text files into one big

text
> files. I was searching on perldoc.org unsuccessfully.
>
> Thanks.
>
> Akbar Ehsan
> Internet Services Coordinator
> Ivy Tech Community College
> 9301 E. 59th St.
> Indianapolis, IN 46216


Sure , just open them one at a time read them in and write them back out
to=20
the destination file one at a time , thousands of way of doing that ,
show us=20
what you got so far and we can help ya along.


Greg


--=20
To unsubscribe, e-mail: beginners-cgi-unsubscribe@perl.org
For additional commands, e-mail: beginners-cgi-help@perl.org
http://learn.perl.org/


Akbar Ehsan

2007-02-02, 7:55 am

Thanks. The cat command worked.

Can I add a line break at the end of the content of each file?=20

I did post my script in my answer to another person who helped.

Akbar Ehsan
Internet Services Coordinator
Central Office
Ivy Tech Community College
9301 E. 59th St.
Indianapolis, IN 46216
Phone: 1 317 921-4872


-----Original Message-----
From: Dennis McFall [mailto:dennis@aceware.com]=20
Sent: Friday, February 02, 2007 1:17 AM
To: Akbar Ehsan; beginners-cgi@perl.org
Subject: Re: Merging Files in Perl

At 08:22 PM 2/1/2007, Akbar Ehsan wrote:
>Hello Everyone,
>
>Is there something in Perl, a procedure, a method, a command, anything,
>that would enable me to merge several tiny text files into one big text
>files. I was searching on perldoc.org unsuccessfully.
>



The cat command in a *nix shell. (Can also be called by a Perl=20
script, if the script is necessary for some other reason):

cat file1 file2 file3 > one_big_file

or

cat file* > one_big_file

Perl:

#!/usr/bin/perl

system("cat somefile* > one_big_file") =3D=3D0 or die "cat failed: =
$?\n";

../dm


--=20
To unsubscribe, e-mail: beginners-cgi-unsubscribe@perl.org
For additional commands, e-mail: beginners-cgi-help@perl.org
http://learn.perl.org/


Sean Davis

2007-02-02, 7:55 am

On Friday 02 February 2007 08:15, Akbar Ehsan wrote:
> Thanks. The cat command worked.
>
> Can I add a line break at the end of the content of each file?


Just print a line break in each file before you close it.
Akbar Ehsan

2007-02-02, 7:55 am

Thanks a lot.=20

Akbar Ehsan
Internet Services Coordinator
Central Office
Ivy Tech Community College
9301 E. 59th St.
Indianapolis, IN 46216
Phone: 1 317 921-4872


-----Original Message-----
From: Sean Davis [mailto:sdavis2@mail.nih.gov]=20
Sent: Friday, February 02, 2007 8:18 AM
To: beginners-cgi@perl.org
Cc: Akbar Ehsan; Dennis McFall
Subject: Re: Merging Files in Perl

On Friday 02 February 2007 08:15, Akbar Ehsan wrote:
> Thanks. The cat command worked.
>
> Can I add a line break at the end of the content of each file?


Just print a line break in each file before you close it.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com