User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Perl section within the Software Development category of DaniWeb, a massive community of 402,787 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,840 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Perl advertiser: Programming Forums
Views: 604 | Replies: 5
Reply
Join Date: Jul 2008
Posts: 2
Reputation: ramesh54 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ramesh54 ramesh54 is offline Offline
Newbie Poster

problem

  #1  
Jul 9th, 2008
Hello Friends,

I am learning Perl now. I have a small query.

I have a directory Z with file name Z.txt.
I would like to copy this file Z.txt to 3 new dir with new filenames
as follows
dir 1 1.txt
dir 2 2.txt
dir 3 3.txt

I would like to then open 1.txt from dir 1 and edit the first 4 lines.

For eg: I have a line as follows in the text.
x y Z
10 11 12

I need to change it as folows

x y z

13 14 15

I need to do the same for all the 3 files.

I have tried the following and I guess there is a much shorter way to
do this

#!/usr/bin/perl
open FILE, "z.txt" or die $!;
while (<FILE>) {
print $_;
}
mkdir("1", 0777) || print $!;
copy("z.txt" ,"1.txt") or die "Copy failed: $!";
move("1.txt" ,"/home/privat/ temp/PerlWD/ 1");
mkdir("2", 0777) || print $!;
copy("z.txt" ,"2.txt") or die "Copy failed: $!";
move("2.txt" ,"/home/privat/ temp/PerlWD/ 2");
mkdir("3", 0777) || print $!;
copy("z.txt" ,"3.txt") or die "Copy failed: $!";
move("3.txt" ,"/home/privat/ temp/PerlWD/ 3");
print " -Done";
exit;

I am not sure how to edit the file and then save it in the respective
file name.

Looking forward to your response.

Regards,
Ramesh
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2006
Posts: 596
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 31
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Posting Pro

Re: problem

  #2  
Jul 9th, 2008
did you give up on the other perl forum?

http://bytes.com/forum/thread819194.html
Reply With Quote  
Join Date: Jul 2008
Posts: 2
Reputation: ramesh54 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ramesh54 ramesh54 is offline Offline
Newbie Poster

Re: problem

  #3  
Jul 9th, 2008
Hi Kevin,

Ho w do I simplify my problem. I went through file management chapters and found only the commands used above. Any hints would be helpful.
Reply With Quote  
Join Date: Nov 2005
Location: Canada
Posts: 236
Reputation: dwks will become famous soon enough dwks will become famous soon enough 
Rep Power: 4
Solved Threads: 20
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: problem

  #4  
Jul 16th, 2008
You can read from and write to the same file at once, but it's difficult.

You're best off creating a copy of the file and modifying that, then replacing the original file with the modified one, as you've done.

You could definitely simplify the code by creating a function which will handle any filename, though.
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote  
Join Date: Mar 2006
Posts: 596
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 31
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Posting Pro

Re: problem

  #5  
Jul 16th, 2008
You can read from and write to the same file at once, but it's difficult.

Actually it is very simple, you can use the Tie::File module to read/write to the same file or perls inplace editor, which would be my choice since it is way more effcient than Tie::File, but Tie::File has an extremely easy interface.
Reply With Quote  
Join Date: Nov 2005
Location: Canada
Posts: 236
Reputation: dwks will become famous soon enough dwks will become famous soon enough 
Rep Power: 4
Solved Threads: 20
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: problem

  #6  
Jul 24th, 2008
Correction: it's difficult in C and C++, but of course Perl would have a module for it. It looks like a very useful one, too . . . .
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Perl Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Perl Forum

All times are GMT -4. The time now is 10:07 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC