Wednesday, April 13, 2011

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

From stackoverflow
  • Try using find instead. And/or xargs.

  • 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.

  • 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
    
  • $ find -maxdepth 1 -type f -newermt 'Mar 4' ! -newermt 'Mar 5' \
           -execdir zgrep 12345 '{}' /dev/null ';'
    
  • 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.

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

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.