Solved

Outputing to a file

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Six Sigma Control Plans

724 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