473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Editing the string

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
8 1768
nithinpes
410 Expert 256MB
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
4,059 Expert 2GB
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
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
410 Expert 256MB
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
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
410 Expert 256MB
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
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
4,059 Expert 2GB
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

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

Similar topics

8
by: Chris Dunaway | last post by:
When using a PropertyGrid, I have an object with a Date property, but I am only interested in the Time portion. How do I make the PropertyGrid allow editing the time only? Just the hours and...
2
by: Dennis Ruppert | last post by:
I created a routine to read and edit the description properties of tables, (the one you see in the database window). It works just fine. This is the basic code behind it, I substituted all my...
1
by: Jeff Petter | last post by:
I can't seem to get the update piece working properly while doing in-place editing. I don't receive any errors, but the update doesn't take place. From the examples I've used as "go bys" it looks...
0
by: tom c | last post by:
I am going through "Walkthrough: Editing and Inserting Data in Web Pages with the DetailsView Web Server Control" found at http://msdn2.microsoft.com/en-us/library/sdba1d59.aspx I am using...
0
by: Frnak McKenney | last post by:
Can I use a bound ComboBox for both browsing and editing? I'm working on a small, standalone database application using Visual C#.NET 2003 and an Access data file. In order to keep the number...
5
by: =?Utf-8?B?QWRhciBXZXNsZXk=?= | last post by:
Hi All, I have a GridView inside the EditItemTemplate of a FormView. Both FormView and GridView are data bound using an ObjectDataSource. When the FormView's ObjectDataSource object has a...
1
by: zivon | last post by:
now for the bigger problam :) I know you pepole hate using OE for sending emails, but its user friendly and its needed in this case... I found on this forum, a code that sends email using OE...
0
by: hanusoft | last post by:
This is an example of editing in DataGrid and Default Paging http://www.hanusoftware.com Html Design Code : - <asp:DataGrid id="DataGrid1" DataKeyField="id" runat="server" Height="224px"...
9
by: sunita jadhav | last post by:
my question is if i type in html textbox on key press event suppose i type 12345 values in textbox then i delete or edit any value of text box suppose i edit 3 and i insert the value 6 at 3 but i...
0
by: ee0jmt | last post by:
Hopefully an easy question: Using vb.net I have opened an xml file (which is encrypted) retreived the file information as a string, carry out some editing of the xml data. I now want to produce a...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.