user3687034
user3687034

Reputation: 343

how to search for a particular string from a .gz file?

I want to search for a particular string from a .gz file containing a text file without extracting in linux terminal. I know how to search for a string from a text file using grep "text to search" ./myfile.txt. But how to make it work for .gz files?

Upvotes: 34

Views: 79169

Answers (4)

tuwilof
tuwilof

Reputation: 587

You can use zcat

zcat ./myfile.txt | grep "text to search"

Upvotes: 0

Ankit Mittal
Ankit Mittal

Reputation: 1379

Already it have been answered, but it could be really helpful if you want to search in multiple .gz files.

For searching in all the .gz files in a specific folder you can use

zgrep "yourString" *

Upvotes: 8

4aRk Kn1gh7
4aRk Kn1gh7

Reputation: 4359

gunzip -c mygzfile.gz | grep "string to be searched"

But this would only work if the .gz file contains text file which is true in your case.

Upvotes: 18

Robby Cornelissen
Robby Cornelissen

Reputation: 97140

You can use zgrep. Usage is similar to grep.

zgrep "pattern" file.gz

From the man page's description:

Zgrep invokes grep on compressed or gzipped files. All options specified are passed directly to grep. If no file is specified, then the standard input is decompressed if necessary and fed to grep. Otherwise the given files are uncompressed if necessary and fed to grep.

Upvotes: 67

Related Questions