sventhan
asked on
Perl Script to merge multiple files into one single file...
Dear Experts -
file names
------------
hfospsm.5809.txt
HFOSPSM_1939.TXT
HFOSPSM_319001.TXT
hfospsm_38D5.txt
hfossm.5809.txt
HFOSSM_1939.TXT
HFOSSM_319001.TXT
hfossm_38D5.txt
Perl script needed to sepearte all the above files into 2 groups by "hfospsm" and "hfossm". Then merge the hfospsm files together as a sigle file and do the same with hfossm files.
I've tried something like below to split the files by the name.
#!/usr/bin/perl -w
$mydir = "/home/ArulsS/hf_perl/data _files";
opendir(DIR, "$mydir");
@allfiles = readdir(DIR);
closedir(DIR);
foreach $file (@allfiles) {
if($file =~ m/^(hfospsm_)/i)
{print "$file\n";
}
elsif ($file =~ m/^(HFOSSM_)/i)
{print "$file\n";}
else {print "Invalid file name given\n";}
}
file names
------------
hfospsm.5809.txt
HFOSPSM_1939.TXT
HFOSPSM_319001.TXT
hfospsm_38D5.txt
hfossm.5809.txt
HFOSSM_1939.TXT
HFOSSM_319001.TXT
hfossm_38D5.txt
Perl script needed to sepearte all the above files into 2 groups by "hfospsm" and "hfossm". Then merge the hfospsm files together as a sigle file and do the same with hfossm files.
I've tried something like below to split the files by the name.
#!/usr/bin/perl -w
$mydir = "/home/ArulsS/hf_perl/data
opendir(DIR, "$mydir");
@allfiles = readdir(DIR);
closedir(DIR);
foreach $file (@allfiles) {
if($file =~ m/^(hfospsm_)/i)
{print "$file\n";
}
elsif ($file =~ m/^(HFOSSM_)/i)
{print "$file\n";}
else {print "Invalid file name given\n";}
}
ASKER
thanks ozo for your quick response.
when i execute the script i got the following error.
all.hfospsm No such file or directory at hf_concat.pl line 7.
when i execute the script i got the following error.
all.hfospsm No such file or directory at hf_concat.pl line 7.
Note that this could also be done on the command line with:
cat `find . -iname hfospsm\*` > all.hfospsm
cat `find . -iname hfossm\*` > all.hfossm
(or on dos):
type hfospsm* > all.hfospsm
type hfossm* > all.hfossm
cat `find . -iname hfospsm\*` > all.hfospsm
cat `find . -iname hfossm\*` > all.hfossm
(or on dos):
type hfospsm* > all.hfospsm
type hfossm* > all.hfossm
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
$mydir = "/home/ArulsS/hf_perl/data
opendir(DIR, "$mydir") or die "$mydir $!";
@allfiles = readdir(DIR);
closedir(DIR);
@ARGV=grep/^hfospsm_/i,@al
open STDOUT,"all.hfospsm" or die "all.hfospsm $!";
print while <>;
@ARGV=grep/^hfossm_/i,@all
open STDOUT,"all.hfossm" or die "all.hfossm $!";
print while <>;