Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

perl file help

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

618 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