?
Solved

Outputing to a file

Posted on 1998-11-01
5
Medium Priority
?
164 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 150 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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
Suggested Courses

765 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