Thursday, January 20, 2011

Read the File With Comma separte and copy the files to directory using Perl script

To read the File and copy the files to the directorywith comma separate need the the Package

  FileHandle.


#!/usr/bin/perl

use FileHandle;

if (@ARGV != 2) {
    print  " the argument is invalided \n"   ;
  exit 1;
}

$file_name = shift;
$output_path = shift;

print "$file_name ,$output_path \n";
$dir1 = "/opt/test";

open (FILE, "$file_name") or die "Cannot open $file_name $!\n";

@fields;
while () {
      chomp;
    @fields = split(/\,/);
   
      foreach my $item (@fields) {
      #print "$item  \n";
   }

    if ($#fields < 1) {
        print " Error in file format \n";
        next;
    }
   
   $file_id = $fields[4];
    print  " $file_id \n";        

    `cp -r "$dir1/$file_id" "$output_path/$file_id" `;

 }
close (FILE);



give permissons to file of script and

perl test.pl new.txt  /opt/test/copy

new.txt file format
1,wer,2,root,welcome.txt
2,wee,3,root,welcome1.log
3,333,4,root,sysd.log

No comments:

Post a Comment