You code is searching through the array @commands_run, I am puzzled by your reference to "line".
if it finds pattern somewhere in the array and not whether it finds it in a line ( if that makes sense)
Sorry, but its does not make sense to me. Even if your array contains multi-line records, if it is on a line then it is in the array, you are not looking anywhere else. Maybe you need to post more code?