Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

save output of program in another file

by bingalee (Acolyte)
on Jun 06, 2013 at 16:44 UTC ( #1037486=perlquestion: print w/ replies, xml ) Need Help??
bingalee has asked for the wisdom of the Perl Monks concerning the following question:

i"m running a program on a file, and i want to save its output in another file. how can i do that? thanks :)

Comment on save output of program in another file
Re: save output of program in another file
by perlfan (Curate) on Jun 06, 2013 at 16:49 UTC
    3 argument open or print to STDIN and redirect to a file at the commandline.
      um could you plz correct this?
      open(IN,"100.fastq"); open(OUT,">100-out.fastq"); while($raw=<IN>) { print $raw; $raw=~s/\n|\r//g; @inf=split(//,$raw); for($i=0;$i<scalar(@inf)-15;$i++) { print $inf[$i]; } print "\n"; <>; } close (OUT); close(IN);
      what do i do in the command line? did i do it right?

        What environment are you running in? "Command line" is pretty vague.

        A few pointers here:

        use strict; use warnings;
        is a great way to start a script in Perl since without them Perl won't complain much about what you are doing other than gross syntax errors. With the strictures in place you get told about more subtle issues that can help you get your code working the way you expect it to.
        open THING,"<some_file.ext" or die "some_file.ext:$!"; open OTHERTHING,">some_other_file.ext" or die "some_other_file:$!";
        In my humble opinion the one of the ten commandments of programming in any language is "Thou Shalt Catch Errors." Catching errors is another great step towards programming nirvana where you code does what you expect. There could very well be permission issues either reading a file or writing a file. Your code may not be executing where you think it is. All this and more can cause you issues.

        Finally: what are you expecting your code to do in the first place? What does your input look like?


        Peter L. Berghold -- Unix Professional
        Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: save output of program in another file
by pvaldes (Chaplain) on Jun 06, 2013 at 20:54 UTC

    print to outfile.txt:

    open $MYFILE, '>', "outfile.txt"; print $MYFILE "blah blah blah"; ### don't put "," after $MYFILE close $MYFILE;

    what do i do in the command line?

    i.e. in bash, you make a pipe with ">"

    perl thisismygreatperlscript.pl > everythingwasfine-im-the-output.txt 2>somethingwaswrong-im-the-log.log

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1037486]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2014-09-18 15:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (116 votes), past polls