Solved

Outputing to a file

Posted on 1998-11-01
5
156 Views
Last Modified: 2010-03-04
I would like to read in a file and output the values to
another file.

It will do a while loop for reading and base on some condition it will ouput to OUTPUT file for that particular line read in.

Pls help.
0
Comment
Question by:slok
  • 2
  • 2
5 Comments
 

Accepted Solution

by:
wayhigh earned 50 total points
ID: 1205865
open FILE, "yourfile.conf";
open OUTFILE, ">>outfile.txt";
foreach $line (<FILE>) {
if (your conditions here) {
print OUTFILE "$line";
}
close FILE;
close OUTFILE;

If you give me more info I might be able to help you more..
0
 
LVL 3

Author Comment

by:slok
ID: 1205866
I would like to write to my $dumpfile, pls help.
My codes attached.
===============================================
$fsafile = '/path/input.txt';
$dumpfile = '/path/out.txt';

open (INFILE, $fsafile) or die "open file: $!";
open (OUTFILE, $dumpfile);

$i = 0;
while( <INFILE> ) {

        $i++;
// line 1 do, represent soem header
        if ($i==1) {
                $Header1 = $_;
                print "$Header1\n";
        }
        elsif ($i==6) {
                $Header2 = $_;
                print "$Header2\n";
        }
// WRITE to output file here.... from line 11 to (EOF - 1)
       elsif ($i>10) {
                $Body = $_;
                print OUTPUT "$Body\n";
        }
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 1205867
# comments start with # not //
0
 
LVL 3

Author Comment

by:slok
ID: 1205868
I would like to write to my $dumpfile, pls help.
My codes attached.
===============================================
$fsafile = '/path/input.txt';
$dumpfile = '/path/out.txt';

open (INFILE, $fsafile) or die "open file: $!";
open (OUTFILE, $dumpfile);

$i = 0;
while( <INFILE> ) {

        $i++;
// line 1 do, represent soem header
        if ($i==1) {
                $Header1 = $_;
                print "$Header1\n";
        }
        elsif ($i==6) {
                $Header2 = $_;
                print "$Header2\n";
        }
// WRITE to output file here.... from line 11 to (EOF - 1)
       elsif ($i>10) {
                $Body = $_;
                print OUTPUT "$Body\n";
        }
}
0
 

Expert Comment

by:wayhigh
ID: 1205869
The only thing really wrong with that code is that you're not setting your output file as writeable via redirect or otherwise. It's a good idea to chop the linewraps off the input you're reading in (if it has them). And your statement "print OUTPUT "$Body\n";" should have been "print OUTFILE "$Body\n";".
Here's the code I just tested for you..
-------------------------------------------------------
$fsafile = 'input.txt';
$dumpfile = '>out.txt';

open (INFILE, $fsafile) or die "open file: $!";
open (OUTFILE, $dumpfile);

$i = 0;
while( <INFILE> ) {

  $i++;
chop;
# line 1 do, represent soem header
  if ($i==1) {
    $Header1 = $_;
    print "$Header1\n";
  }
  elsif ($i==6) {
    $Header2 = $_;
    print "$Header2\n";
  }
# WRITE to output file here.... from line 11 to (EOF - 1)
  elsif ($i>10) {
    $Body = $_;
    print OUTFILE "$Body\n";
  }
}

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
Perl Regular expression 9 199
cpan issue 1 61
Search in text file in column and compare 4 46
Call Shell Script from Perl Script 6 78
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 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

21 Experts available now in Live!

Get 1:1 Help Now