Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

removing lines by number



7:54 pm on Jun 26, 2004 (gmt 0)

10+ Year Member

Is there a script that will remove lines by numbers or by name. I've go a back up and I need to remove a line and can not do manually do to size. If so can you give me a name



7:58 pm on Jun 26, 2004 (gmt 0)

Theory..can you please explain a little more about what you want to do?

Are you saying you want to remove lines in a database?


5:18 am on Jun 27, 2004 (gmt 0)

10+ Year Member

I Have a back of mysite in sql. And it appears that line 4114 wont import. So I want to to take it out of the backup and insert a fixed version of the line after. So I want to remove line 4114 and don't know if there is script that will do this? If there is would anyone know of the name. Or a script I can read the back up with.


4:21 pm on Jun 27, 2004 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Welcome to WebmasterWorld Theory,

you could make one pretty quickly.

I would write from your existing file to a new file if it really is that big so that you don't have to keep so much in memory.

$srcfile = "/path/to/source/file.sql";
$destfile = "/path/to/destination/file.sql";
$sfp = fopen($srcfile,'r');
$dfp = fopen($destfile,'w');
// I used an array here, if you have more than one line to remove
// include all line numbers to remove seperated by commas
$lineskip = array(4114);
$linecount = 1;
// I don't know the size of the biggest row of data
// but just make the 1024 bigger if need be
while ($nextline = fgets($sfp,1024)) {
if(!in_array($linecount,$lineskip)) {

something like that should work fine. Hopefully the file isn't so long that the script times out but it shouldn't.


6:51 pm on Jun 27, 2004 (gmt 0)

10+ Year Member

That's how I've handled problems in big sql files in the past, and it's good to see it affirmed by jatar_k as probably the simplest and fastest solution.

While you're in the loop, however, after jatar_k's

if(!in_array($linecount,$lineskip)) {  

consider throwing in an else statement like

else echo "'".$nextline."'<br>\n";

so you can see what the problem is with the line.

Then you can add an ereg_replace() to fix whatever is wrong, and run the script again, without losing any data. Like:

if(!in_array($linecount,$lineskip)) {  
else {
$nextline = ereg_replace('correction','problem',$nextline);

Featured Threads

Hot Threads This Week

Hot Threads This Month