in reply to next statement logic

AFAICS (and providing $entry is somewhat sensible derived from $line in your real script) the snippet fits your description.

The next in the 2nd if will take care that you don't write to OUTPUT1 again (in case you already did in the 1st if).

The last next isn't necessary, since you are already at the end of the loop body there.