LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-21-2009, 08:12 AM   #1
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Rep: Reputation: 15
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
 
Old 08-21-2009, 08:42 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650
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");
 
Old 08-26-2009, 12:56 AM   #3
nanda22
Member
 
Registered: Mar 2008
Posts: 58

Original Poster
Rep: Reputation: 15
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.
 
Old 08-26-2009, 08:02 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Xen Problem while creating guest " Invalid file location given: No such file " mgurnani Linux - Software 0 05-09-2009 04:02 PM
An error occurred - no valid devices werefound on which to crate new file systems. zzznaufalc Linux - Newbie 2 05-05-2009 12:09 PM
location of #include file? ragsnayak Linux - Software 2 11-17-2008 06:55 AM
C header file location? Sambojambo Programming 2 02-18-2008 09:45 AM
crate a super user account sampathnd Linux - General 1 05-27-2005 12:32 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration