0

I have a very large text file with over 100,000 lines in it. I need to collect/skip a set number of lines: loop through lines 1-100, skip lines 101-150, read lines 151-210, skip lines 211-300 (for example).

I have the following code

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

foreach ($lines as $lineNumber => $line) {

    $counter++;

    if ($counter < 101) {
         //Do update stuff
    }

    if ($counter < 102 && $counter > 151) {
         //Skip these lines
    }

    if ($counter < 152 && $counter > 211) {
         //Do update stuff
    } 
}

Is there a better way to skip over many lines of an array's output?

6
  • Not a good idea to have a large file in memory Commented Aug 24, 2017 at 20:25
  • Don't use foreach in this situation, use for and utilize the counter Commented Aug 24, 2017 at 20:25
  • Read file line by line with fread Commented Aug 24, 2017 at 20:27
  • Do your skip lines interval is fixed or dynamic??? Commented Aug 24, 2017 at 20:35
  • The original idea was to skip lines after I iterated over a line that didn't match criteria. example; line began with A, I'd get the following 50 lines. If I iterate a line that starts with B, I'd ignore the next X amount of lines until I crossed an A again. Rinse and repeat. Commented Aug 24, 2017 at 20:36

1 Answer 1

2

First, move to fgets, it is memory efficient way. You don't need to have all the array in memory. As for conditions, just combine all your conditions with or operator and don't add condition for skipping, it's useless.

if ($counter < 101 || ($counter >= 151 && $counter <= 210) || add another pediods here) {
     //Do update stuff
}

P.S. you have a mistake in your conditions, ($counter < 102 && $counter > 151) is always false as well as the other one.

Sign up to request clarification or add additional context in comments.

3 Comments

Using fgets, can I utilize 'w' instead of 'r' so I can write to the file once I've done my logic handling?
You are the driver) You can, of course. Use fseek to walk in it.
@AdamA: Not if you value your data. If you want to use fgets, write your updated data to a second file. (You can move it over top of the input file once your processing loop is done.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.