Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 08-21-2009, 08:12 AM   #1
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.

open (MYFILE, 'filelist.txt');

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

while (<MYFILE>)
 if ($c == $lin)
      #print @line;
      foreach $word (@line)
          my $var4= qx(perl;
          print $var4; 
      } # for end
    close (MYFILE);

   } # 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
Senior Member
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 652Reputation: 652Reputation: 652Reputation: 652Reputation: 652Reputation: 652
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



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

# Inside the loop
system("echo $line >> /my/new/file");
Old 08-26-2009, 12:56 AM   #3
Registered: Mar 2008
Posts: 58

Original Poster
Rep: Reputation: 15
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
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,569

Rep: Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427Reputation: 2427
Actually, the approved Perl method is


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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:54 PM.

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