tags:

views:

632

answers:

5
+2  Q: 

zcat pipe to grep

ls -ltr|grep 'Mar  4'| awk '{print $9 }'|zcat -fq |grep  12345

I want to find all files modified on a certain date and then zcat them and search the fiels for a number string.

the above doesn't work because it searches the file name for the string not the file itself.

Any help?

M

+1  A: 

Try using find instead. And/or xargs.

leppie
+2  A: 

Use xargs to run the grep on the filenames output by awk:

ls -ltr | grep 'Mar 4' | awk '{print 9}' | xargs zcat -fq | grep 12345

Or, I guess, run the rest of the pipe from awk itself.

unwind
+1  A: 

If I read your command line correctly, its zcat that is trying to unpack the filenames instead of their content. Use xargs to solve this:

ls -ltr|grep 'Mar  4'| awk '{print $9 }'|xargs zcat -fq |grep  12345
cg
A: 
$ find -maxdepth 1 -type f -newermt 'Mar 4' ! -newermt 'Mar 5' \
       -execdir zgrep 12345 '{}' /dev/null ';'
ashawley
A: 

In addition to the fine answers about using xargs or find, you can also get rid of an extra process by eliminating zcat and piping to zgrep or grep -Z instead.

dwc
On my system (OS X) grep -Z doesn't do what you appear to think it does.
Chris Lutz
Apparently not on the Linux I have handy, either. Oops. zgrep is the better choice, then.
dwc