Maybe OT - Grepping the locate command

Kevin Hunter hunteke at earlham.edu
Wed Mar 3 14:11:36 GMT 2010


At 6:36am -0500 Wed, 03 Mar 2010, donn wrote:
> On 03/03/2010 13:08, John McCabe-Dansted wrote:
>> so we need something a bit longer like. locate *.ods | while read f ;
>> do unzip -p $f | grep BLAH>  /dev/null &&  echo $f ; done
> 
> Groovy. I added quotes to handle weird filenames:
> locate *.ods | while read f; do unzip -p "$f" | grep -i "gas" >/dev/null 
> && echo "$f"; done

Actually, even that can get you in to trouble.  If you want to be
robust, consider using a delimiter that *can't* be in the filename.  For
example, the NUL byte:

SAVE_IFS="$IFS"
IFS=$'\0'  # temporarily change how bash delimits information

# now do your business as usual.  Note there's now no need to quote $f
locate -0 *.ods | while read f; do unzip -p $f | \
  grep -i "gas" > /dev/null && echo $f; done

IFS="$SAVE_IFS" # restore normal bash delimiters

* untested, off the top of my head, beware, etc.

Kevin



More information about the sounder mailing list