Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

perl file help

Posted on 2011-03-07
5
Medium Priority
?
402 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
  • 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 27

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 27

Accepted Solution

by:
wilcoxon earned 2000 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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Introduction to Processes

810 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