Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

how to redirect output in different files

by utpalmtbi (Acolyte)
on Jun 14, 2013 at 06:30 UTC ( [id://1038899]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks..

I am trying to replace characters in file through perl regex like :

open my $hfile, $ARGV[0] or die "Can't open $ARGV[0] for reading: $!" +; while( my $line = <$hfile> ) { $line =~ s/\s+/\n/g; print $line; } close $hfile;

I have to run this script for all the files in my folder so I use following command described previously in this forum as

for %f in (*.txt) do @yourscript.pl %f

It works fine but I want to save the output generated by each input file in a subfolder. For example if my current folder contains the files named list1.txt, list2.txt etc.. an 'outfile' subfolder should generate and the results for each file shouls ve saved in it as out1.txt, out2.txt etc.. But I don't know how to do that..Plz help

Replies are listed 'Best First'.
Re: how to redirect output in different files
by space_monk (Chaplain) on Jun 14, 2013 at 06:46 UTC
    You can use File::Basename to break out the part of your filename you are interested in.
    use strict; use warnings; use File::Basename; for my $infile (@ARGV) { my $fname = basename( $infile); # replace the part of the filename you want to change $fname =~ s/list/out/; # add the target directory to it. my $outfile = 'outfile/'.$fname; open my $ih, "<$infile" or die("Cannot open $infile"); open my $oh, ">$outfile" or die("Cannot open $outfile"); { local $/ = undef; <$ih>; # slurp the file # sure you want to replace whitespaces with returns? # also may need to alter this as operates on whole file now s/\s+/\n/g; # global replace print $oh; } close $ih; close $oh; }
    If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)
Re: how to redirect output in different files
by poj (Abbot) on Jun 14, 2013 at 06:58 UTC
    #!perl use strict; my $infile = $ARGV[0]; (my $outfile = $infile) =~ s/^list/out/; $outfile = 'outfile/'.$outfile; open my $h_in,'<',$infile or die "Can't open $infile for reading: $!"; open my $h_out,'>',$outfile or die "Can't open $outfile for writing: $ +!"; print "$infile -> $outfile \n"; while( my $line = <$h_in> ) { $line =~ s/\s+/\n/g; print $h_out $line; } close $h_in; close $h_out;
    poj
Re: how to redirect output in different files
by jwkrahn (Abbot) on Jun 14, 2013 at 06:46 UTC
    for %f in (*.txt) do @yourscript.pl %f

    Try it like this:

    perl -i -pe's/\s+/\n/g' *.txt
      That would require changing $ARGV[0] to shift also.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-24 00:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found