•
•
•
•
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
![]() |
•
•
Join Date: Jul 2008
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
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
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
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.
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
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
•
•
•
•
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.
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 . . . .
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
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
![]() |
•
•
•
•
•
•
•
•
DaniWeb Perl Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Installing Windows 98 On VMware. Floppy problem (Windows 9x / Me)
- Windows XP keeps restarting since a new video card (Windows NT / 2000 / XP / 2003)
- Redhat Linux 6.2 - ipop3d problem? (*nix Software)
- Problem with T720 (Gadgets and Gizmos)
- Connection Problems (Networking Hardware Configuration)
- Encoding (Unicode) problem in IE 6.0 (Web Browsers)
- .htaccess mod_rewrite problem (Linux Servers and Apache)
- Javascript/HTML problem!!! (JavaScript / DHTML / AJAX)
- Problem with Windows Update and WinXP (Web Browsers)
Other Threads in the Perl Forum
- Previous Thread: Form processing problem
- Next Thread: Can't seem to find the problem...


Linear Mode