Solved

Outputing to a file

Posted on 1998-11-01
5
157 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…

831 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