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?
Use this (untested):
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 * z *grep to handle compressed files, and then * ze *grep 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". I hope that is clear!
Thanks and kudos to other contributors involved discussion below.
You can use zgrep
cd /my_home
zgrep 'vid=123' *log.20140226*
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.