appending files
Anonymous Monk
created: 2006-01-03 17:14:05
Hi all,
I need some help with the following script. I have a text file (invs_to_load.txt that contains the filenames of all the files I want to append into a single file (this file should contain the name of the 1st file in the list possibly).
I am aware that I should be using unix cat command to achieve this but I am not sure if it is possible to loop through the file and append each file or even if this is the best solution. I have included my script here to provide a better idea of what I'm doing. Thank you in advance for your help.
$dirtoget = "/tmp/invs";
opendir(FILEDIR, $dirtoget) || die ("Cannot open $dirtoget");
@thefiles = grep -T, <$dirtoget/*>;  #Store the filenames into an array
closedir(FILEDIR);

#Calling another perl script that takes each filename as argument 
#and splits the file into multiple files for further processing

foreach $file (@thefiles) {    
@split_files = `perl split_invs.pl $file`) || next;  
chomp(@split_files);

$temp = join(" ", @split_files); 
`/tmp/get_acct_info.sh $temp`;  #This script takes each split file and reads some information & 
				#outputs the info to the file output_acct_list.out


#Load the main accounts file into a hash to compare against another file 
#and output accounts only in output_accts_list

$origaccts = "/tmp/main_accounts.txt";
open(FILEHANDLE, $origaccts) || die ("Cannot open $origaccts");
%lookup = map {chomp; $_ => undef} ;
close(FILEHANDLE);

$acctlist = "/tmp/output_acct_list.out"; #Open the file output_acct_list for reading
open(ACCTLISTFILE, $banlist) || die ("Cannot open $acctlist");

#Output file that will contain filename of the bills that should be loaded into our system
open($output, '>', '/tmp/invs_load.txt') || 
die ("Cannot open output file /tmp/invs_load.txt");

while ($line = ){
  chomp $line;
  ($filename, undef, undef, undef, $account) = split /\t/, $line;
  next if exists $lookup{$account};
  print $output "$filename\n";   #Print the filename of bills to be loaded to invs_load.txt
}

 

	}
Re: appending files
created: 2006-01-03 17:30:18

A one-liner

perl -ne"print qx[perl -pe1 $_]" test.lst > test.all

Where test.lst contains the list of files 1 per line and their contents is concatenated into test.all

Update: This is a better one that avoids using large amounts of memory for large files:

perl -ne"system 'perl.exe', '-pe1', $_;"  test.lst >test.all

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: appending files
created: 2006-01-03 21:28:06
Thank you both for your response.
Unfortunately, I didn't fully understand Smokemachine's script was doing.
BrowserUK, When I ran the above code, I continue to get the following error (for both lines): Use of uninitialized value in concatenation (.) or string at testscr.pl
As I am calling this within another perl script, I added backticks to the beginning and the end of the statement. But everything else remains the same (given that I am using the actual filenames within this line of code).
I've also tried the following code which seems to compile without any errors but the appended file is blank. Can someone help me see what I'm doing wrong here?
#accts_to_load.txt contains the filenames of al the files I want to append together
$acctstoload = "/apps/tmp/invs/accts_to_load.txt";
open(ACCTLOAD, $acctstoload) || die("cannot open $acctstoload");

#Here, I want to give a specific name to the file all the split files are being merged into
$batchfile = "mergedfile.txt";

while()  {
`cat $_ >> $batchfile`;
}
I'm not sure what i'm doing wrong. Any suggestions would be appreciated. Thanks.
Re^3: appending files
created: 2006-01-04 00:26:24
Unfortunately, I didn't fully understand Smokemachine's script was doing.

Did you write the script you are attempting to modify or extend?

Which is more important to you in the short term?

  • Getting this script to work.
  • Learning how to make this script work yourself?

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
Re: appending files
created: 2006-01-03 17:49:42
Could be this?
#!/usr/bin/perl -n
BEGIN{die "file please" unless $ARGV[0];}
push @a,$_;
END{
open(FILE, ">>".shift @a);
foreach(@a){
open(FILE2, "<$_");
while(){
print FILE $_;
}close FILE2}
}

perlmonks.org content © perlmonks.org and Anonymous Monk, BrowserUk, smokemachine

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03