homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

removing lines by number

10+ Year Member

Msg#: 4220 posted 7:54 pm on Jun 26, 2004 (gmt 0)

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




Msg#: 4220 posted 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?


10+ Year Member

Msg#: 4220 posted 5:18 am on Jun 27, 2004 (gmt 0)

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.


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

Msg#: 4220 posted 4:21 pm on Jun 27, 2004 (gmt 0)

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.


10+ Year Member

Msg#: 4220 posted 6:51 pm on Jun 27, 2004 (gmt 0)

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);

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved