Solved

perl file help

Posted on 2011-03-07
5
391 Views
Last Modified: 2012-06-27
Hello,
This is my intention.
I need to open 1 to many files and write to one.
Any help would be appreciated.

if (@ARGV != 1)
{
        print "\n\tUsage: $0 file\n\n";
        exit 2112;
}

my ($file) = @ARGV or
               die "Can't open file: error $!\n";
open (LOG, $file);
open (RESULT, ">today.logsummary");
0
Comment
Question by:fac66
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:tel2
ID: 35062669

Hi fac66,

> I need to open 1 to many files and write to one.
Please explain exactly what you mean by the above.

Thanks.
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35062723
We need more information to answer your question.  Is the one file specified on the command line an input file or the output file?  How should the file names be gotten (for both input and output)?

This will read file names from the command line.  The first one is the output file.  All additional ones are input files.
#!/usr/local/bin/perl

use strict;
use warnings;

my $out = shift;
die "Usage: $0 <output file> <input files ...>\n" unless ($out and @ARGV);

open OUT, ">$out" or die "could not create $out: $!";
foreach my $in (@ARGV) {
    open IN, $in or die "could not open $in: $!";
    while (<IN>) {
        chomp;
        # do whatever with the contents of the input
        # to write to the output file:
        # print OUT $something;
    }
    close IN;
}

Open in new window

0
 

Author Comment

by:fac66
ID: 35062973
Thanks for your help.
I am trying to read 1 to many log files via command line.
Then write out a summary to 1 file called log.summary.

I can't get passed the usage statement:

Usage: ./stat25 <output file> <input files ...>
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 35063030
If it's a fixed output filename, this should work.  Usage would just be:

./stat25 file1 file2 file3 ...

#!/usr/local/bin/perl

use strict;
use warnings;

die "Usage: $0 <input files ...>\n" unless @ARGV;

open OUT, '>log.summary' or die "could not create log.summary: $!";
foreach my $in (@ARGV) {
    open IN, $in or die "could not open $in: $!";
    while (<IN>) {
        chomp;
        # do whatever with the contents of the input
        # to write to the output file:
        # print OUT $something;
    }
    close IN;
}

Open in new window

0
 

Author Comment

by:fac66
ID: 35063257
I got it to work... thank you!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Progress

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question