简体   繁体   中英

Fast grep multiple values from a compressed file

Are there any better method than the following command?

zgrep ABC test.tgz | grep DEF | grep GHI | ..

By better I mean performance and ease of use (ie type the command from memory)

I seems to that you do an and in any order.

cat file
ABC
ABC DEF
ABC DEF GHI
ABC GHI DEF
DEF


zgrep ABC file | grep DEF | grep GHI
ABC DEF GHI
ABC GHI DEF

This can be done like this

awk '/ABC/ && /DEF/ && /GHI/' file # here patter can be in any order
ABC DEF GHI
ABC GHI DEF


awk '/ABC.*DEF.*GHI/' file  # here patterns need to be in that order.
or
grep "ABC.*DEF.*GHI" file
ABC DEF GHI


grep is not good to do and in any order.

Of the top of my head I would say no. You are grepping for results from within the results of a previous grep so the pipes are the best way to do this without additional code which would lower the performance.

这是一个sed一个内衬做相同的事情,应该比多个grep更快:

sed -n -e '/ABC/{/DEF/{/GHI/p;};}' file

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM