Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl file handling 1

Status
Not open for further replies.

ptsri1954

Technical User
Dec 11, 2003
5
IN
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;
}
}
========================





 
You need to chomp each line before you do the compare. Remember, at the end of each line in a file is the return characters to get to the next line.

- Rieekan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top