Reputation: 15372
I want to do grep some lines from files I'm finding with the find
command. The files are zipped though. How can I find them, de-compress them, do my grep search and put my results into a new file.
zcat find /my_home -name '*log.20140226*' | grep 'vid=123'
is not working, give the error:
gzip: invalid option -- 'e'
Try `gzip --help' for more information.
I assume though that if it were working, to save I should add
> ~/found_logs.20140226.txt
Is this correct?
What is wrong with my zcat
and grep
string?
Upvotes: 0
Views: 1897
Reputation: 207455
Use this:
find /my_home -name '*log.20140226*' -exec zgrep -H 'vid=123' {} \; > results
Or, if you want to find files with lines containing your 2 different strings, in any order:
find /my_home -name '*log.20140226*' -exec zegrep -H "vid=23.*mid=2|mid=2.*vid=23" {} \; > results
Notes:
The -H
option causes grep
to print the name of the matching file.
I use zgrep to handle compressed files, and then zegrep to handle the regular expression of "pattern|pattern" wherein I specify the search strings in both possible orders on the line, so I am effectively searching for EITHER of two patterns, namely "vid followed by mid" OR "mid followed by vid".
Upvotes: 4
Reputation: 4079
You need to use:
zcat `find /my_home -name '*log.20140226*'`|grep 'vid=123'
Without the backticks find /my_home ...
is being sent as arguments to zcat
, when you want the output of the find
to be sent as arguments to zcat
.
Upvotes: 1