LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   crate a file with same name in new location (https://www.linuxquestions.org/questions/linux-newbie-8/crate-a-file-with-same-name-in-new-location-749271/)

nanda22 08-21-2009 07:12 AM

crate a file with same name in new location
 
Hi All,
I've got a program which reads a file and replaces some text with new text. After this how can i create a file with same name but in new location?

The $var4 contains my original file name.

Code:

open (MYFILE, 'filelist.txt');

$lin = 1;
$c = 1;
$var1 = 0;
$var2 = 0;
$var3 = 0;
$var4 = 0;
$var5 = 0;

while (<MYFILE>)
{
 if ($c == $lin)
    {
      @line=$_;
      #print @line;
     
      foreach $word (@line)
      {
        chomp($word);
        ($var1,$var2,$var3,$var4,$var5)=split(/\\/,$word);
          my $var4= qx(perl modifiedfile.pl);
          print $var4;
      } # for end
         
    close (MYFILE);
    exit;
 
    }
  $c++; 

  } # While
         
close (MYFILE);

Here i got stuck with how to copy this $var4 file content with filename to new location?, can anyone give some clue

kbp 08-21-2009 07:42 AM

Hi nanda22,

You just need to add something like:

# Before the while loop
open(OUTHNDL, ">/path/to/new/$var4") || die "Could not create file.\n";


# Inside the loop
print OUTHNDL "$line\n";


# After the loop
close(OUTHNDL);

cheers,

kbp

P.S. .. or the really ugly hack:

# Inside the loop
system("echo $line >> /my/new/file");

nanda22 08-25-2009 11:56 PM

Hi KBP,
Thaks a lot for your reply. it worked nicely.
I just modified it little bit, placed
open(OUTHNDL, ">/path/to/new/$var4") || die "Could not create file.\n";
inside the loop, because when i placed it outside, it was creating file name as "0", as $var4 value is been assigned to 0 at first. so when i placed it inside, it took value which i captured from another file.
it is working fine. Thank you very much for your kind reply.

chrism01 08-26-2009 07:02 PM

Actually, the approved Perl method is
http://search.cpan.org/~dapm/perl-5....b/File/Copy.pm


All times are GMT -5. The time now is 09:31 AM.