Solved

Outputing to a file

Posted on 1998-11-01
5
153 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
Comment Utility
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
Comment Utility
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
Comment Utility
# comments start with # not //
0
 
LVL 3

Author Comment

by:slok
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

15 Experts available now in Live!

Get 1:1 Help Now