Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/forums/perl/)
-   -   Manipulating a txt file with Perl (http://www.go4expert.com/forums/manipulating-txt-file-perl-t17078/)

redfox 23Apr2009 20:15

Manipulating a txt file with Perl
hello everyone, im new to perl and hoping for someone to help me...i have a file of the form :- file.txt, the contents are as follows:-
2009-03-13 00:03:10 7599171 2639128 6480404 2639 0023
2009-03-13 00:03:14 48020 4478014 2341060 4478 0182
2009-03-13 00:03:17 44476 4916057 2620157 4917 0000

what im hoping to do is to write a perl script that will remove the whitespaces and replace with a , delimiter so that it looks like this:-


The perl script should be able to process this file in a directory say /jumbo/in and once the file is processed it should be moved to /jumbo/out

Attached is my perl script that i have come up with needless to say it doesnt work:-


use strict;                                                           
use warnings;                                                         
use File::Copy;                                                       
my @files=</jumbo/in/'>;                                                                                                                                                     
while ( <*.txt> ){                                                   
    open (FH,$_) or die "failed to read $_: $!";                     
    while(my $line = <FH>){                                         
        open(OUT,">>" ,"temp") or die "failed open temp for write' $!";                                                         
        @bits = split(/\s+/,$line);                                   
        for ($i=0;$i < $#bits;$i++)                                   
        if ($i < 5 ) { print "$bits[$i],"; }                         
        else { print "$bits[$i] "; }                                 
    print OUT $line;                                                 
    move("temp","/jumbo/out/".$_) or die "cannot rename temp:$!\n"; 
    unlink $_ or die "Cannot remove $_:$!\n";                       

shabbir 23Apr2009 20:21

Re: Manipulating a txt file with Perl
Moved to PERL forum

murugaperumal 25Feb2010 10:59

Re: Manipulating a txt file with Perl
Dear Friend,

You can use the following coding. But it is working for only file. because I have used only one file.

The file contents are available in the following file /jumbo/in/file.
The following program also available in the same directory.

Then I have created another file in the directory /jumbo/in/. That file is used to write the content after the replace the space as ",".


use strict;
use warnings;
open(FH,'<file') or die "$! can't open";
open (FH1,'>file1') or die "$! can't open";
my $var;
    $var=~s/ /,/g;
    print FH1 $var;
system('mv file1/jumbo/out/');

But the above code only work for only one file. If you want to perform the operation multiple files or single file. But your code will show me as multiple files.

All times are GMT +5.5. The time now is 01:57.