Optimisation of a "search and grab" shell script