467,108 Members | 1,330 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,108 developers. It's quick & easy.

problem in manupliating the file ???

Hi All,

Requirement :
1.read the content of the dir
2.seach for "prstat-Lvs" file
3.rename this file to *.txt (as this file doesn't have the file typeextension --raw file)
4.open this *.txt file & search for K&M
5.replace all K instance to *3 (manuplication)
6.replace all M instance to *4 (manuplication)
7.stored this calcuation in new file
8.rename this new file back to raw file
9.delete all the *.txt files

i have acheived this half a way (till step no 7) but the problem is, if i have more than 1 prstat-Lvs file, then code doesn't gives the correct raw files but instead it will give only 1 correct raw file & other one will be the replica of another.

Expand|Select|Wrap|Line Numbers
  1. my $dir = "C:\\Performance_svap\\INPUT_FILES\\*";
  2. my @file=glob("$dir");
  3. my $f;
  4. my $f2;
  5. my $oldfile;
  6. my @newfile;
  7. my @txtfile;
  8.         foreach $f (@file){
  9.             if ($f =~ /prstat-Lvs/){
  10.                 push (@prstat_Lvs,$f);    
  11.             foreach my $a (@prstat_Lvs){
  12.                 $oldfile = $a;
  13.                 my $newfile = $f . ".txt";
  14.                 rename($f,$newfile);
  15.                 push(@txtfile,$newfile);
  16.                 open (FH1,"$newfile") or die "Can't open $newfile : $!";
  17.                 $f2 = $a."new";
  18.                 $f2= $f2 . ".txt"; 
  19.                 open FH2, '>', $f2 or die "Couldn't open $f2: $!"; 
  20.                 while (<FH1>) { 
  21.                     @a_list=split(' ', $_);
  22.                     $new_line=" ";
  23.                     foreach $entry (@a_list) {
  24.                         if($entry =~ /K$/) {
  25.                             chop($entry);
  26.                             $entry=$entry*3;
  27.                         }
  28.                         if($entry =~ /M$/) {
  29.                             chop($entry);
  30.                             $entry=$entry*4;
  31.                     }
  32.                         $new_line=$new_line."    ".$entry;
  33.                 }
  34.                     print FH2 "$new_line\n";
  35.             }
  36.         }
  37.  
  38.                 close FH1;
  39.                 push(@newfile,$f2);
  40.                 close FH2;
  41.                 foreach my $k(@newfile){
  42.                     rename($k,$a);
  43.                 }
  44.                 foreach my $j(@txtfile){
  45.                     unlink $j;
  46.                 }
  47.                 }    
  48. }
  49.  
can i avoid step 7, instead just open the *.txt--do calculation--replace the value in the same file & rename it back to orginial raw file name ???

Thanks,
Vijayarl
Sep 30 '08 #1
  • viewed: 1361
Share:
1 Reply
nithinpes
Expert 256MB
can i avoid step 7, instead just open the *.txt--do calculation--replace the value in the same file & rename it back to orginial raw file name ???

Thanks,
Vijayarl
If you are going to change the file back to it's raw format, you need not change the extension to .txt. You can straightaway open the file, modify and write result to a temp file and later rename the temp file back to original file.




i have acheived this half a way (till step no 7) but the problem is, if i have more than 1 prstat-Lvs file, then code doesn't gives the correct raw files but instead it will give only 1 correct raw file & other one will be the replica of another.
That is because of:
Expand|Select|Wrap|Line Numbers
  1. if ($f =~ /prstat-Lvs/){ 
  2.  
This line will take both 'prstat-Lvs' and 'prstat-Lvs.txt'. It should be:
Expand|Select|Wrap|Line Numbers
  1.  foreach $f (@file){ 
  2.             if (($f =~ /prstat-Lvs/)&&($f !~ /\.txt/)){ 
  3.                 push (@prstat_Lvs,$f);     
  4.             foreach my $a (@prstat_Lvs){ 
  5.                 $oldfile = $a; 
  6.                 my $newfile = $a . ".txt"; 
  7.  
Sep 30 '08 #2

Post your reply

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

Similar topics

7 posts views Thread by Keith Dewell | last post: by
11 posts views Thread by Marcus Jacobs | last post: by
11 posts views Thread by Abhishek | last post: by
15 posts views Thread by Ken Allen | last post: by
4 posts views Thread by Salad | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.