By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,933 Members | 1,676 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,933 IT Pros & Developers. It's quick & easy.

Editing the string

P: 37
Hello All,

I just have a small query. I am just curious to know if I could have this desired output.

My input file is r145_AF_FD_z_ HJ.txt

So finally my output should be

Expand|Select|Wrap|Line Numbers
  1. /abc/ FD/ r145_AF_FD_abc_ HJ.txt
  2. /pqt/ FD/r145_AF_FD_ pqt_HJ.txt
  3. /sde/ FD/r145_AF_FD_ sde_HJ.txt
I guess prefix and suffix should be added along with the directory name. Any help or suggestion will be helpful.

I have tried this so far
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl 
  2. use strict;
  3. use File::Path;
  4. use File::Copy;
  5. my @dirs = qw[ F:/usr/eg/abc/FD F:/usr/eg/pqt/FD F:/usr/eg/sde/FD];
  6. for my $dir (@dirs) {
  7. mkpath($dir) or die $!;
  8. }
  9. chomp(my $file = <>);
  10.  
  11. for my $dir (@dirs) {
  12. my ($rename) = $dir =~ m!/(\w+)/FD$!;
  13. copy($file, "$dir/$rename.txt") or die $!;
  14. }
  15.  
Regards,
Ramesh
Jul 23 '08 #1
Share this Question
Share on Google+
8 Replies


nithinpes
Expert 100+
P: 410
First, the line
Expand|Select|Wrap|Line Numbers
  1. chomp(my $file = <>);
  2.  
will open the file and get it's first line assigned to $file. But, I beleive what you want is to get the filename.


Replace:
Expand|Select|Wrap|Line Numbers
  1. chomp(my $file = <>);
  2.  
  3. for my $dir (@dirs) {
  4. my ($rename) = $dir =~ m!/(\w+)/FD$!;
  5. copy($file, "$dir/$rename.txt") or die $!;
  6. }
  7.  
with:

Expand|Select|Wrap|Line Numbers
  1. chomp(my $file = $ARGV[0]);
  2.  
  3. for my $dir (@dirs) {
  4. my $fix;
  5. if($dir=~/\/(.+)\/FD$/) {
  6. $fix= $1;
  7. }
  8. my $rename = $file;
  9. $rename =~ s/z/$fix/;
  10. copy($file, "$dir/$rename") or warn "$file:$!";
  11. }
  12.  
This should meet your requirement.
Jul 23 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
I'm not sure what he is doing either but this is one way to get the filenames how he wants:

Expand|Select|Wrap|Line Numbers
  1. my $file = 'r145_AF_FD_z_HJ.txt'; 
  2. my @dirs = qw[ F:/usr/eg/abc/FD F:/usr/eg/pqt/FD F:/usr/eg/sde/FD];
  3. for my $dir (@dirs) {
  4.    my $t = $file;
  5.    my ($var1,$var2) = $dir =~ m!/((\w+)/FD)$!;
  6.    $t =~ s/z/$var2/;
  7.    $t = "$var1$t";
  8.    print $t,"\n";
  9. }
  10.  
output:

Expand|Select|Wrap|Line Numbers
  1. abc/FDr145_AF_FD_abc_HJ.txt
  2. pqt/FDr145_AF_FD_pqt_HJ.txt
  3. sde/FDr145_AF_FD_sde_HJ.txt
  4.  
Jul 23 '08 #3

P: 37
My requirement was as follows.
1. I need to create 3 new directories abc, pqt & sde with sub directories FD for these folders.
Expand|Select|Wrap|Line Numbers
  1. /abc/FD
  2. /pqt/FD
  3. /sde/FD

2. The program should ask for the input file which is r145_AF_FD_z_ HJ.txt

3. The file need to be copied to all the FD folders,
Expand|Select|Wrap|Line Numbers
  1. /abc/FD/r145_AF_FD_z_ HJ.txt
  2. /pqt/FD/r145_AF_FD_z_ HJ.txt
  3. /sde/FD/r145_AF_FD_z_ HJ.txt
4. I need to replace the file name in the respective folders without altering the prefix (r145_AF_FD_) & suffix(_ HJ.txt) and replace the z with the respective directory name.
Expand|Select|Wrap|Line Numbers
  1. /abc/FD/r145_AF_FD_abc_ HJ.txt 
  2. /abc/FD/r145_AF_FD_pqt_ HJ.txt 
  3. /abc/FD/r145_AF_FD_sde_ HJ.txt 
I tried the below code, but it doesnot give the desired output,

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl 
  2. use strict;
  3. use File::Path;
  4. use File::Copy;
  5. my @dirs = qw[ ../../abc/FD ../../pqt/FD ../../sde/FD];
  6. chomp(my $file = $ARGV[0]);      
  7. for my $dir (@dirs) { 
  8. my $fix;
  9. if($dir=~/\/(.+)\/FD$/) {   
  10. $fix= $1;      
  11. }   
  12. my $rename = $file;      
  13. $rename =~ s/z/$fix/;         
  14. copy($file, "$dir/$rename") or warn "$file:$!";   
  15. }
Jul 23 '08 #4

nithinpes
Expert 100+
P: 410
What is the output/error that you are getting?
Also, there is a white space between _ and HJ in the file r145_AF_FD_z_ HJ.txt. If that is how it is, you need to give the input (file name) within quotes-
"r145_AF_FD_z_ HJ.txt"
If that does not work, try to print $rename and post the output or error message that you are getting.
-Nithin
Jul 24 '08 #5

P: 37
Sorry. There is no white space in the input file. It was a typing error.
input file is

r145_AF_FD_z_HJ.txt

the error I am getting is illegal division by zero at line 14.

I had given the input file without quotes.
Jul 24 '08 #6

nithinpes
Expert 100+
P: 410
Sorry. There is no white space in the input file. It was a typing error.
input file is

r145_AF_FD_z_HJ.txt

the error I am getting is illegal division by zero at line 14.

I had given the input file without quotes.
Well, if you don't have spaces within the argument then no need go give input within quotes.
The error message is strange. I tried the same code in my Windows machine and it works fine.
Did you try to print out $rename and check what value it is getting?
Jul 24 '08 #7

P: 37
I have printed out $rename, The first few lines are the rror i am getting and last last line refers to the &rename.The code used is as follows,

#!/usr/bin/perl
use strict;
use File::Path;
use File::Copy;
my @dirs = qw[ ../../abc/FD ../../pqt/FD ../../sde/FD];
for my $dir (@dirs) {
mkpath($dir) or die $!;
}
chomp(my $file = $ARGV[0]);
for my $dir (@dirs) {
my $fix;
if($dir=~/\/(.+)\/FD$/) {
$fix= $1;
}
my $rename = $file;
$rename =~ s/z/$fix/;
print $rename;
copy($file, "$dir/$rename") or warn "$file:$!";
}

r145_AF_FD_z_HJ.txt:No such file or directory at sample11.pl line 18.
r145_AF_FD_z_HJ.txt:No such file or directory at sample11.pl line 18.
r145_AF_FD_z_HJ.txt:No such file or directory at sample11.pl line 18.
r145_AF_FD_../abc_HJ.txtr145_AF_FD_../pqt_HJ.txtr145_AF_FD_../sde_HJ.txt
Jul 24 '08 #8

KevinADC
Expert 2.5K+
P: 4,059
you will have to be in the directory where $file is for it to work like you have it. Otherwise use the full directory path to $file instead of just the filename. I showed you how to convert those filename in my other post. I leave it up to you how to incorporate my suggestion into your working code.
Jul 24 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.