output to STDOUT to a file

J

joe shaboo

I'm trying to output the STDOUT to a file which is the same as the
file I'm reading.

The way I'm handling it now is sloppy, and I'd like to do it all
inside the perl script.

Here's an example.

$file = filename

while (<$file>) {
print $_;
}

Then from the command line, I do $./script file > file1;mv file1 file

This is terrible, and I know it is easy, but I can't get it to work
with open(), and I'm sure you can, I just can't get it to work.

Can anyone help me out?
 
P

Paul Lalli

I'm trying to output the STDOUT to a file which is the same as the
file I'm reading.

The way I'm handling it now is sloppy, and I'd like to do it all
inside the perl script.

Here's an example.

$file = filename

while (<$file>) {
print $_;
}

Then from the command line, I do $./script file > file1;mv file1 file

This is terrible, and I know it is easy, but I can't get it to work
with open(), and I'm sure you can, I just can't get it to work.

Can anyone help me out?


First, what do you mean by "can't get it to work". What happens that
shouldn't? What doesn't happen that should? What errors and warnings to
you receive? What does your code actually look like?

Second, read
perldoc perlrun
and look for the -i and -p switches. If what you're doing is relatively
simple, there's a good chance those two switches can combine to reduce
your program to one line.
(and even if they can't, you can still use those switches in your shebang
to reduce the amount of code you actually have to write)

Paul Lalli
 
R

Richard Morse

I'm trying to output the STDOUT to a file which is the same as the
file I'm reading.

The way I'm handling it now is sloppy, and I'd like to do it all
inside the perl script.

Here's an example.

$file = filename

while (<$file>) {
print $_;
}

Then from the command line, I do $./script file > file1;mv file1 file

This is terrible, and I know it is easy, but I can't get it to work
with open(), and I'm sure you can, I just can't get it to work.

I think, if I understand you correctly, that you are trying to do
in-place file editing. That is, you wish to open a file, read through
it, and save any changes. The best way to do this is something like:

#!/usr/bin/perl
use strict;
use warnings;

my $filename = 'some_file_name';
my $backup = $filename . ".backup";
rename($filename, $backup) or die("Can't backup original file: $!");
open(my $in, "<", $backup) or die("Can't open backup: $!");
open(my $out, ">", $filename)
or die("Can't open original for writing: $!");

while(<$in>) {
print $out $_;
}

close($in);
close($out);

__END__

Perl can actually do most of this, if you use the various command line
options (I think -i, -n, or -p are the appropriate ones to look to). In
fact, if your task fits into the paradigm provided by the command line
options, it is probably much faster.

HTH,
Ricky
 
D

David K. Wall

Paul Lalli said:
I'm trying to output the STDOUT to a file which is the same as
the file I'm reading.

The way I'm handling it now is sloppy, and I'd like to do it all
inside the perl script.
[snip]

Second, read
perldoc perlrun
and look for the -i and -p switches. If what you're doing is
relatively simple, there's a good chance those two switches can
combine to reduce your program to one line.

Additionally, see perlfaq5, in particular the entry titled "How can I
use Perl's -i option from within a program?"

A quick search of the FAQ by the OP could have easily missed this
entry, which is a good reason why reading the entire FAQ (or at least
skimming it) is a good idea. I need to reread it myself, as I suspect
there's been stuff added since I last read it.
 
R

Robin

joe shaboo said:
I'm trying to output the STDOUT to a file which is the same as the
file I'm reading.

The way I'm handling it now is sloppy, and I'd like to do it all
inside the perl script.

Here's an example.

$file = filename

while (<$file>) {
print $_;
}

Then from the command line, I do $./script file > file1;mv file1 file

This is terrible, and I know it is easy, but I can't get it to work
with open(), and I'm sure you can, I just can't get it to work.

Can anyone help me out?

can I see some more of your code? -Thanks, Robin
 
M

Matt Garrish

Richard Morse said:
I think, if I understand you correctly, that you are trying to do
in-place file editing. That is, you wish to open a file, read through
it, and save any changes. The best way to do this is something like:

[snip code that reads from backup and saves to original]

The best way would arguably be to use the Tie::File module for in-place
editing. The only caveat I would add is to only use the module on data you
trust. I loved the module so much after using it for the first time that I
tried incorporating it into a script that does some prepocessing work on
files for the editorial staff where I work. Sure enough, up it went in smoke
the first time it was used on a file that had been emailed from an outside
author (damn line endings!).

So I guess that means your way is the safest, albeit not the coolest. But
there's always something good to be said for safe code.

Matt
 
J

Joe Smith

joe said:
I'm trying to output the STDOUT to a file which is the same as the
file I'm reading.

C:\> perl -pi.bak -e "s/foo/bar/g" file1.txt file2.txt
unix% perl -pi -e 's/foo/bar/g' file1 file2

Or open the file for read+write, read entire file, seek() to the
beginning, write new data, truncate, close.

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,149
Messages
2,570,842
Members
47,388
Latest member
EarthaGilm

Latest Threads

Top