Solved

perl file help

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mapAB Challlenge 35 123
python question 5 69
Arduino EDI - Programming language 3 79
Advice on Xojo as a development tool over VB. 4 31
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
A short article about problems I had with the new location API and permissions in Marshmallow
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

929 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

11 Experts available now in Live!

Get 1:1 Help Now