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

To delete first few letters in a string in a file

P: 12
I have got a file called filelist with content
Expand|Select|Wrap|Line Numbers
  1. f+    f    templatedata\poc\Ravi\Appfolder\filelist.xml    
  2. f+    f    templatedata\poc\Ravi\Appfolder\testdeploy.xml    
  3. f    -    templatedata\poc\Ravi\Appfolder\testdeploy2.xml    
  4. f    -    templatedata\poc\Ravi\Webfolder\apples.bmp    
  5.  
I need to remove the first few characters in the begining and need to have only
Expand|Select|Wrap|Line Numbers
  1.  
  2. templatedata\poc\Ravi\Appfolder\filelist.xml
  3. templatedata\poc\Ravi\Appfolder\testdeploy.xml
  4. templatedata\poc\Ravi\Appfolder\testdeploy2.xml
  5. templatedata\poc\Ravi\Webfolder\apples.bmp
  6.  
so that i can compare the files and write the appfolder and webfolder contents to different files...but because of these unwanted characters f+ f ,I am not able to compare,.........how to remove these unwanted letters from the begining

My code for comparing is as follows
Expand|Select|Wrap|Line Numbers
  1. open (MyFileHandle,"<C\:\\home\\filelist.txt") or die("File Not Found\n");
  2. open (MyWebFileHandle,">C\:\\home\\Weblist.txt") or die("File Open Error");
  3. open (MyAppFileHandle,">C\:\\home\\Weblist.txt") or die("File Open Error");
  4.  
  5. until((eof(MyFileHandle)) && (seek(MyFileHandle,0,0) != eof(MyFileHandle)))
  6. {
  7.  
  8.     $Text = readline(MyFileHandle);
  9.     if(defined($Text)) 
  10.     {
  11.         if ($Text=~/^templatedata\\poc\\Ravi\\Appfolder/) 
  12.         {
  13.          print MyAppFileHandle $Text;
  14.          print MyAppFileHandle "\n";
  15.         }
  16.         elsif ($Text=~/^templatedata\\poc\\Ravi\\Webfolder/)
  17.         {
  18.         print MyWebFileHandle $Text;
  19.         print MyWebFileHandle "\n";
  20.         }
  21.             }
  22. }
  23.  
Mar 12 '08 #1
Share this Question
Share on Google+
1 Reply


numberwhun
Expert Mod 2.5K+
P: 3,503
I have not tested your code, but I went a completely different route and chose to use the split function, which is part of Perl. Here is what I tried that works:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. my $field1;
  5. my $field2;
  6. my $field3;
  7.  
  8. open(FILE, "<data.txt") or die "Cannot open data.txt:  $!";
  9. open(FILE2, ">newdata.txt") or die "Cannot oen newdata.txt: $!";
  10.  
  11. while(<FILE>)
  12. {
  13.     ($field1, $field2, $field3) = split(/\s+/, $_);
  14.     print FILE2 ("$field3\n");
  15. }
  16.  
  17.  
  18. close(FILE);
  19. clost(FILE2);
  20.  
Also, when you place text or code in the forums, please be sure to enclose it in code tags as I have done to your original post. This makes for better readability.

Regards,

Jeff
Mar 12 '08 #2

Post your reply

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