Link to home
Create AccountLog in
Avatar of sventhan
sventhanFlag for United States of America

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";}
}
Avatar of ozo
ozo
Flag of United States of America image

#!/usr/bin/perl -w
$mydir = "/home/ArulsS/hf_perl/data_files";
opendir(DIR, "$mydir") or die "$mydir $!";
@allfiles = readdir(DIR);
closedir(DIR);
@ARGV=grep/^hfospsm_/i,@allfiles ;
open STDOUT,"all.hfospsm" or die "all.hfospsm $!";
print while <>;
@ARGV=grep/^hfossm_/i,@allfiles ;
open STDOUT,"all.hfossm" or die "all.hfossm $!";
print while <>;
Avatar of sventhan

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.

Avatar of Adam314
Adam314

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
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer