Quote and then trust
Most mysterious shell bugs are word splitting or globbing on a value you did not expect. Quoting every expansion removes that family of bugs, and the remaining failures are real logic you can read.
Small inputs first
Run the script against three files rather than three thousand. A bug that shows on three is the same bug, and the run takes a second instead of a minute and a half of unreadable output.