Link to home
Create AccountLog in
Avatar of Meena BBC
Meena BBCFlag for United States of America

asked on

How to send output from perl script to a text file in PERL?

Hi. I am working on executing a script in PERL. I have it running and currently shows the data in the command prompt terminal.
How can I send this to a text or excel file?
Right now, I have perl report.pl (to run script)

Not sure how I can enter command to follow to show output in a file it can be named temp.txt
perl report.pl > print temp.txt (something close to this)?

Seems like this would be easy.
ASKER CERTIFIED SOLUTION
Avatar of Dr. Klahn
Dr. Klahn

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
 b) Input prompts could still be sent to STDERR
This isn't specific to Perl at all. You can use the standard Unix I/O redirection feature (which has been adopted by all major operating systems.

To redirect the output from a process to a file, use '>' (note that this overwrites anything that was already in the file).
perl report.pl > temp.txt

Open in new window

To redirect the output from a process and append it to the data in a file, use '>>'.
perl report.pl >> temp.txt

Open in new window

Note that these only redirect output that is sent to STDOUT. Anything that is written to STDERR will still appear on the console. This is usually what you want, but you can redirect STDERR as well, using slightly more complex syntax.
perl report.pl &> temp.txt

Open in new window

There are plenty of other things you can do. See I/O Redirection for more details.