problem in manupliating the file ???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vijayarl
    New Member
    • Sep 2008
    • 65

    #1

    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.

    Code:
    my $dir = "C:\\Performance_svap\\INPUT_FILES\\*";
    my @file=glob("$dir");
    my $f;
    my $f2;
    my $oldfile;
    my @newfile;
    my @txtfile;
    		foreach $f (@file){
    			if ($f =~ /prstat-Lvs/){
    				push (@prstat_Lvs,$f);	
    			foreach my $a (@prstat_Lvs){
    				$oldfile = $a;
    				my $newfile = $f . ".txt";
    				rename($f,$newfile);
    				push(@txtfile,$newfile);
    				open (FH1,"$newfile") or die "Can't open $newfile : $!";
    				$f2 = $a."new";
    				$f2= $f2 . ".txt"; 
    				open FH2, '>', $f2 or die "Couldn't open $f2: $!"; 
    				while (<FH1>) { 
    					@a_list=split(' ', $_);
    					$new_line=" ";
    					foreach $entry (@a_list) {
    						if($entry =~ /K$/) {
    							chop($entry);
    							$entry=$entry*3;
    						}
    						if($entry =~ /M$/) {
    							chop($entry);
    							$entry=$entry*4;
    					}
    						$new_line=$new_line."	".$entry;
    				}
    					print FH2 "$new_line\n";
    			}
    		}
    	
    				close FH1;
    				push(@newfile,$f2);
    				close FH2;
    				foreach my $k(@newfile){
    					rename($k,$a);
    				}
    				foreach my $j(@txtfile){
    					unlink $j;
    				}
    				}	
    }
    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
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    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:
    Code:
    if ($f =~ /prstat-Lvs/){
    This line will take both 'prstat-Lvs' and 'prstat-Lvs.txt'. It should be:
    Code:
     foreach $f (@file){ 
                if (($f =~ /prstat-Lvs/)&&($f !~ /\.txt/)){ 
                    push (@prstat_Lvs,$f);     
                foreach my $a (@prstat_Lvs){ 
                    $oldfile = $a; 
                    my $newfile = $a . ".txt";

    Comment

    Working...