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

I am very new to Perl and searched all over for code and help, but none of the codes I wrote works. I have a lot of subscriber files in a directory called subscriberdata
Each file (subscribername.txt) has the subscribername, telephone, email and language in the files separated by ","
I am trying to write a basic script that will read all the files in the subscriberdata directory and then produce ONE file (subscriber.dat) in the following format:
THE FIRST LINE NOT REALLY NECESSARY
subscribername,telephone,email,language
name,5555,test@test.com,english
name,48545,test1@test1.com,afrikaans
so that all subsribers information is in one file which I can then import into Excel
I know this is very basic, but I am not a programmer and just started with CGI. I am teaching myself and still at the very basics.
I would really appreciate help. Best regards David
  • Comment on Writing one File from Various Files in a Directory

Replies are listed 'Best First'.
Re: Writing one File from Various Files in a Directory
by BrowserUk (Patriarch) on Jun 21, 2003 at 00:20 UTC

    If I understand you correctly,

    perl -pe"BEGIN{@ARGV = map{ glob} @ARGV; print'subscribername,telephon +e,email,language', $/;}" *.txt > subscribers.all

    would probably do the job.

    But that said, if you simply want to concatenate all the files into one big file

    copy con subscribers.all subscribername,telephone,email,language ^Z type *.txt >>subscribers.all

    would also do it.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Re: Writing one File from Various Files in a Directory
by aquarium (Curate) on Jun 21, 2003 at 00:22 UTC
    they look like exactly the same format as the output file, just "cat" them all together from DOS/Unix prompt or you can do same from perl
    dos prompt> type *.txt >>outputfilename
    unix prompt> cat *.txt >>outputfilename
    Perl (dos/unix prompt)> perl -e "{`system cat $_ >>outputfilename`}" *.txt
    haven't tested the perl version so you may need do adjust a little.
Re: Writing one File from Various Files in a Directory
by WhiteBird (Hermit) on Jun 21, 2003 at 00:32 UTC
    If I understand correctly, you want to loop through all the files in one directory, pull the text out of those files and stick the text into another file? I did a similar thing on a Windows box and found the following code worked for me:
    my $dir = "location of your file directory"; my $filename = "the location & name of your new file"; open (LOGFILE, ">$filename") or die "Can't create logfile: $!"; opendir (DATADIR, $dir) or die "Can't open $dir: $!"; while( defined ($file = readdir DATADIR) ) { next if $file =~ /^\.\.?$/; open (INFILE, "$dir\\$file") or die "Can't open $dir: $!"; print LOGFILE "$file|"; $/ = ''; while (<INFILE>) { @lines = $_; } foreach (@lines) { print LOGFILE $_; } } close (INFILE);
    It's been awhile since I've used this snippet, but it might get you started. I did take the data in the LOGFILE and import it into an Access database, so it will probably go into Excel without too much trouble.

    Also, I'm pretty new with Perl, so more than likely there's a better, shorter way to do it that a more experienced Monk could share. HTH,
    WB

      Just wondering why you would set $/ to the null string?

      First I would suggest doing so in a local-context (although it doesn't hurt in such a small script as yours) so it gets automatically "reset" when you leave the enclosing scope.

      By setting $/ to the null string, the input record separator becomes one or more blank lines (multiple blank lines gets collapsed into one).

      If you just want to slurp the whole of the file at once (without collapsing multiple blank lines into one as a side effect), do a

      undef $/;
      and then you can drop the whole
      while (<INFILE>) {@lines = $_;} foreach (@lines) {print LOGFILE $_;}
      loop and just do a
      $line=<INFILE>; print LOGFILE, $line;

      Staying closer to your script

      while (my $line=<INFILE>){print LOGFILE,$line;}
      is somewhat more elegant, IMHO.

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

        Thanks for the input on this point, Count. I originally used this script to get name/address information off of cover sheets used for mailing reports. I needed to slurp the information on each page into one line. However, I think the question in this case is going to require your approach. ++For the explanation.