Solved

perl file help

Posted on 2011-03-07
5
357 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 11

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This is about my first experience with programming Arduino.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now