Chris202 has asked for the wisdom of the Perl Monks concerning the following question:

Dear all I own nothing but faith in you all because I have tried to find the answer to my question on the internet for long but couldn't get (or understand) it... Here's my problem: I have a script that scans throught a text file and writes all line except those starting with an A.
#!/usr/bin/perl use strict; use warnings; open (my $file, "<", "/file.txt") or die "cannot open < file.txt $!"; while (<$file>) { unless (/^A/) { print; } }
That works, but I get the results of this script in the terminal. What I want is just to get these results to be saved in a new text file. Can somebody help me ? Please light my path amid the darkness with the wisdom that is yours Thanks a lot ! Chris

Replies are listed 'Best First'.
Re: how to extract script output in new text file ?
by davido (Cardinal) on Oct 25, 2014 at 18:12 UTC
Re: how to extract script output in new text file ?
by Loops (Curate) on Oct 25, 2014 at 17:31 UTC

    Hi Chris202 and welcome to the monastery.

    You can use an open command very similar to the one that already exists in your program, except switching the less-than to a greater-than symbol to indicate output. Then use the Perl select command to indicate that you want print output to go there instead of STDOUT (which is usually the console).

    use strict; use warnings; open (my $output, '>', 'output.txt') or die "cannot open > output.txt +$!"; select $output; open (my $file, '<', 'file.txt') or die "cannot open < file.txt $!"; while (<$file>) { print unless (/^A/) } select *STDOUT; print "All Done.\n";

    The print unless ... idiom in the example above is a more terse way to write the same thing you had in your program.

Re: how to extract script output in new text file ?
by 2teez (Vicar) on Oct 25, 2014 at 17:52 UTC

    ...What I want is just to get these results to be saved in a new text file. ..
    That can easily be done by printing your desired output to a file filehandle, you have opened like so:

    use strict; use warnings; # please note the 'sign' ">" open( my $fh, ">", "output_file.txt" ) or die "cannot open output_file +. +txt$!"; open( my $file, "<", "/file.txt" ) or die "cannot open < file.txt $!"; while (<$file>) { unless (/^A/) { # please note "$fh" filehandle to write to print $fh $_; } } close $file or die $!; close $fh or die $!;

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: how to extract script output in new text file ?
by uatrigger (Novice) on Oct 25, 2014 at 21:43 UTC
    Hi Chris202!
    Easy way to save your output when you run your script in terminal is:
    $ perl your_script.pl > report.txt
    The report.txt file will have all what script have found.
    Ofcourse if you don't want to know more about perl.
Re: how to extract script output in new text file ?
by CountZero (Bishop) on Oct 26, 2014 at 15:44 UTC
    or use Perl's command line switches "n" and "e":
    perl -ne "print unless/^A/;" file.txt > newfile.txt
    That is for Windows.

    In Unix it is probably more like:

    perl -ne 'print unless/^A/;' ./file.txt > ./newfile.txt

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics