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.
PerlScripting Languages

Avatar of undefined
Last Comment
Dave Cross
ASKER CERTIFIED SOLUTION
Avatar of Dr. Klahn
Dr. Klahn

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of ozo
ozo
Flag of United States of America image

 b) Input prompts could still be sent to STDERR
Avatar of Dave Cross
Dave Cross
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Scripting Languages
Scripting Languages

A scripting language is a programming language that supports scripts, programs written for a special run-time environment that automate the execution of tasks that could alternatively be executed one-by-one by a human operator. Scripting languages are often interpreted (rather than compiled). Primitives are usually the elementary tasks or API calls, and the language allows them to be combined into more complex programs. Environments that can be automated through scripting include software applications, web pages within a web browser, the shells of operating systems (OS), embedded systems, as well as numerous games. A scripting language can be viewed as a domain-specific language for a particular environment; in the case of scripting an application, this is also known as an extension language.

30K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo