Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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 arra +y 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 fi +le #and output accounts only in output_accts_list $origaccts = "/tmp/main_accounts.txt"; open(FILEHANDLE, $origaccts) || die ("Cannot open $origaccts"); %lookup = map {chomp; $_ => undef} <FILEHANDLE>; close(FILEHANDLE); $acctlist = "/tmp/output_acct_list.out"; #Open the file output_acct_li +st for reading open(ACCTLISTFILE, $banlist) || die ("Cannot open $acctlist"); #Output file that will contain filename of the bills that should be lo +aded into our system open($output, '>', '/tmp/invs_load.txt') || die ("Cannot open output file /tmp/invs_load.txt"); while ($line = <ACCTLISTFILE>){ 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 lo +aded to invs_load.txt } <Here I want to retrieve the filenames from the above file and append +these files (in order) into a single file prior to loading it into ou +r system> }

Replies are listed 'Best First'.
Re: appending files
by BrowserUk (Patriarch) on Jan 03, 2006 at 22:30 UTC

    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.
      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 ap +pend 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(<ACCTLOAD>) { `cat $_ >> $batchfile`; }
      I'm not sure what i'm doing wrong. Any suggestions would be appreciated. Thanks.
        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
by smokemachine (Hermit) on Jan 03, 2006 at 22:49 UTC
    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(<FILE2>){ print FILE $_; }close FILE2} }