Reputation: 1197
E.g. having the following file.gz:
dbc
1
321
d53 8
I can use the following step-by-step approach to grep for regex, show the line number and so on (well, grep is kinda mighty):
gunzip file.gz; grep -Pn "^\d{2,}$" file; gzip file
Output will be 3:321
.
This will uncompress the file, grep for patterns, compress the file. So I have to have the permission to write data. I don't want to write data and there has to be a smarter way to handle compressed files.
One can easily find zgrep which says that options are passed directly to grep. Still, I can't use regex (just POSIX expressions) with zgrep. I also don't know if zgrep will internally be doing the same like the step-by-step mentioned above.
So how can I use advanced grep options for compressed files directly, without uncompressing them?
Upvotes: 0
Views: 2304
Reputation: 9417
zgrep
is the Right Thing in this case. If you need to do something similar for other commands that don't have a zgrep
equivalent, you can generally do something like this:
gunzip -c blah.gz | some_command
or even more general
cat blah.gz | gunzip | some_command
This works with any decompressor that inputs and outputs on stdin/stdout (which is most of them).
Upvotes: 1
Reputation: 89557
You can easily do the same using a POSIX pattern:
zgrep -n "^[0-9]\{2,\}$" zipzip.gz
But if you absolutly want to use a perl regex:
zgrep -nP "^\d{2,}$" zipzip.gz
Upvotes: 2
Reputation: 33
Are you using the -E flag? From the manpage:
-E, --extended-regexp Interpret PATTERN as an extended regular expression (see below).
Upvotes: 0