Page 1 of 2

[SOLVED] Deleting a line in a txt file

Posted: Mon Jan 17, 2005 10:23 am
by siefkencp
Any one know how? I want to use the line as data for a query and then delete it after I'm done with it.

Thanks.
Chris

Posted: Mon Jan 17, 2005 10:28 am
by feyd
you have to load the entire file into memory and rewrite the file sans that line.

Posted: Mon Jan 17, 2005 10:37 am
by siefkencp
Can I do that with the while loop?

Check out what I have so far its bits from other code I'm modifying becuase what I was using timed out the server below are both examples.

Working on this one:

Ok it wont let me insert my code...

Posted: Mon Jan 17, 2005 10:44 am
by feyd
it's likely you have something in the code, which the server denies posting... like INSERT INTO, .htaccess, things of this nature.

if you have one of the instant messengers I have listed below, you can IM me with some of the details, and I can tell you how to get it posted. :)

Posted: Mon Jan 17, 2005 11:42 am
by siefkencp
Old code... (this works but too slow) and I took out the T and the O

Code: Select all

<?
include 'connect.php';
$number = 0;
$snumber = $number + 500;
$cbss_array = mysql_query ("SELECT `Lastname` , `Firstname` , `key` , `ZIP` FROM `cbss`")
	or die ("Not Found1" . mysql_error());
while ($row_cbss = mysql_fetch_array($cbss_array, MYSQL_ASSOC))&#123;
	$lname_cbss = $row_cbss&#1111;'Lastname'];
	$fname_cbss = $row_cbss&#1111;'Firstname'];
	$key = $row_cbss&#1111;'key'];
	$zip = $row_cbss&#1111;'ZIP'];
	$nrccua_array = mysql_query ("SELECT * FROM `nrccua` WHERE `lname` = '$lname_cbss' and `Fname` = '$fname_cbss' and `zipcode` = '$zip'")
		or die ("Not Found2" . mysql_error());
	while ($row_nrccua = mysql_fetch_array($nrccua_array, MYSQL_ASSOC))&#123;
	$lname_nrccua = $row_nrccua&#1111;'lname'];
	$sequence = $row_nrccua&#1111;'sequence'];
	$number = $number + 1;
	mysql_query ("INSER INT `dupe` (`sequence` , `cbsskey`) VALUES ('$sequence' , '$key')")
		or die ("Not Found3" . mysql_error());
	mysql_query ("DELET FROM `nrccua` WHERE `sequence` = '$sequence'")
		or die ("Unsuccessful" . mysql_error());
		print "$lname_cbss, $fname_cbss <br>\n";
		if ($number == $snumber) &#123;
		print "<br><br><b>Deleted thes records from NRCCUA and inserteded the sequence number into NRCCUA_TEMP</b>";
		die;
		&#125;
	&#125;
&#125;
include 'close';
?>

Posted: Mon Jan 17, 2005 11:43 am
by siefkencp
Working on this one:
I want to delete the line in the file after I'm done using it.

Remember there are obviously things I need to do to this one to make it ready...

Code: Select all

include 'connect.php';
$myFile = fopen("sequence.txt", "r");
$myFile_cbss = fopen("key.txt", "r");
$number = 0;
$snumber = $number + 500;

while (!feof($myFile) &#123;
	$line = fgets($myFile, 255);
	$nrccua_array = mysql_query ("SELECT `sequence` FROM `nrccua` WHERE `sequence` = '$line'")
		or die ("Not Found2" . mysql_error());
	while ($row_nrccua = mysql_fetch_array($nrccua_array, MYSQL_ASSOC))&#123;
	$lname_nrccua = $row_nrccua&#1111;'lname'];
	$sequence = $row_nrccua&#1111;'sequence'];
	$number = $number + 1;
	mysql_query ("INSER INTO `dupe` (`sequence` , `cbsskey`) VALUES ('$sequence' , '$key')")
		or die ("Not Found3" . mysql_error());
	mysql_query ("DELET FROM `nrccua` WHERE `sequence` = '$sequence'")
		or die ("Unsuccessful" . mysql_error());
		print "$lname_cbss, $fname_cbss <br>\n";
		if ($number == $snumber) &#123;
		print "<br><br><b>Deleted thes records from NRCCUA and inserteded the sequence number into NRCCUA_TEMP</b>";
		die;
		&#125;
	&#125;
&#125;
include 'close';

Posted: Mon Jan 17, 2005 11:58 am
by siefkencp
Ideally I would use the whole text file at once but since it has 4500 lines im afraid I will hit my 30 second limit again.

Posted: Mon Jan 17, 2005 12:01 pm
by feyd
here's the concept:

Code: Select all

$lines = file('file.txt');

$copy = $lines;

foreach($lines as $ln => $line)
&#123;
  $line = trim($line);
  // use the line in here
  //...
  //...
  //...
  // done using line
  if( used )
    unset($copy&#1111;$ln]);
&#125;

$fp = fopen('file.txt','w');
fwrite($fp, join("\n",$copy));
fclose($fp);
you can avoid the timeout via set_time_limit

Posted: Mon Jan 17, 2005 12:09 pm
by siefkencp
Ok but I will have 4500 lines to parse through and if it stops in the middle of that I wont know where...

Posted: Mon Jan 17, 2005 12:14 pm
by feyd
writing the file for each pass will make the script run very slow.

as I noted, use set_time_limit() to change the timeout.. if you're allowed to.

Posted: Mon Jan 17, 2005 12:24 pm
by siefkencp
I may still run into the browser's time out limit... next choice do it comandline... however i have never tried php on command line...

I am the server's administrator...

Posted: Mon Jan 17, 2005 12:26 pm
by feyd
it's possible you may run into the browser's time limit.. Even if that happens, the script will continue execution until php's time limit is reached.. if you set the time limit to zero, then the script will run until completion.

Posted: Mon Jan 17, 2005 12:33 pm
by siefkencp
I'm skeptical but I will attempt it.

Posted: Mon Jan 17, 2005 1:17 pm
by siefkencp
Different question this time... I'm close now... I just need to know how to read a line and ignore the return charicter my PHP manual tells me that fgets grabs the line plus the return...

Almoast there...

How about just using the file function as an array... I get the feeling you were heading there with your previous post...

Posted: Mon Jan 17, 2005 1:19 pm
by feyd
trim()/rtrim()