The script is producing the correct output, but probably not the output you expected. Since you didn't say (although I could guess) what you expected, we can't tell you why your expectations were wrong or what changes you need to make to get your expected output.

duh! that's completely opposite to how I thought this worked. not only have I never noticed this before it's always worked - I suppose I've never had a program where the same grep expression was repeated.

My previous post was correct, but I failed to understand an important detail. The $_ in grep is not a program variable, but rather an alias for one element of the input array at a time. A separate match position is maintained for each element.

A match is never found for any element except the fifth. The position for those elelments is reset every time.

In your first grep, a match is found for the fifth element. Its position is advanced past the match. The second grep does not find a match because there are not any more matches. The position is reset. The third grep is now has the same situation as the first and gets the same result.

You can get the result you expected by resetting the position of the fifth element each time.