Hi,
I am trying to do the following:
1. Open A file
2. search for a string
3. if string "str1" already exists exit
4. else add the string "str1" and exit
I am baffled because right now....It keeps adding the string "str1" to the file even if the string pattern "str1" exists in it.
Here is the code I am using...any pointers in the right direction will help.Please bear with my perl programming style...right now...Newbie to perl..hacking my way thru the Perl handbook and books.
========================
$PN="$ENV{'CLEARCASE_PN'}";
$CPRT = "Copyright: © 2004 NONSUCHCOMPANY, INC. ALL RIGHTS RESERVED.";
open (INFILE1, $PN) || die ("CANNOT OPEN FILE \n");
while ($line = <INFILE1>)
{
if($line eq 'Copyright: © 2004 NOSUCHCOMPANY, INC. ALL RIGHTS RESERVED.')
{
print "$line";
close(INFILE1);
exit 0;
}
else
{
close(INFILE1);
open (OUTFILE1, ">$PN.temp") ||die ("Cannot open output file $PN.temp\n");
print OUTFILE1 ("* Copyright: $CPRT\n");
open (INFILE1, $PN) || die ("Cannot open input file $PN.\n");
$line1 = <INFILE1>;
while ($line1 ne "") {
@field = split(' ', $line1);
if ($field[0] ne "#_ClearCase_Info") {
print OUTFILE1 ("$line1");
}
$line1 = <INFILE1>;
}
close (OUTFILE1);
close (INFILE1);
rename ( "$PN.temp" , "$PN" );
exit 0;
}
}
========================
I am trying to do the following:
1. Open A file
2. search for a string
3. if string "str1" already exists exit
4. else add the string "str1" and exit
I am baffled because right now....It keeps adding the string "str1" to the file even if the string pattern "str1" exists in it.
Here is the code I am using...any pointers in the right direction will help.Please bear with my perl programming style...right now...Newbie to perl..hacking my way thru the Perl handbook and books.
========================
$PN="$ENV{'CLEARCASE_PN'}";
$CPRT = "Copyright: © 2004 NONSUCHCOMPANY, INC. ALL RIGHTS RESERVED.";
open (INFILE1, $PN) || die ("CANNOT OPEN FILE \n");
while ($line = <INFILE1>)
{
if($line eq 'Copyright: © 2004 NOSUCHCOMPANY, INC. ALL RIGHTS RESERVED.')
{
print "$line";
close(INFILE1);
exit 0;
}
else
{
close(INFILE1);
open (OUTFILE1, ">$PN.temp") ||die ("Cannot open output file $PN.temp\n");
print OUTFILE1 ("* Copyright: $CPRT\n");
open (INFILE1, $PN) || die ("Cannot open input file $PN.\n");
$line1 = <INFILE1>;
while ($line1 ne "") {
@field = split(' ', $line1);
if ($field[0] ne "#_ClearCase_Info") {
print OUTFILE1 ("$line1");
}
$line1 = <INFILE1>;
}
close (OUTFILE1);
close (INFILE1);
rename ( "$PN.temp" , "$PN" );
exit 0;
}
}
========================