How to use the command zegrep (with examples)
zegrep is a command that allows you to search for extended regular expression patterns in compressed files using egrep
. It supports various options such as case-sensitive or case-insensitive search, matching only lines that do not match a pattern, printing file names and line numbers for each match, printing only the matched text, and recursively searching files within a compressed file.
Use case 1: Search for extended regular expressions in a compressed file (case-sensitive)
Code:
zegrep "search_pattern" path/to/file
Motivation: This use case is useful when you want to search for specific extended regular expressions in a compressed file, while considering the case sensitivity of the search.
Explanation:
zegrep
: This is the command used to search for extended regular expressions in compressed files."search_pattern"
: This is the pattern you want to search for in the file.path/to/file
: This is the path of the compressed file you want to search in.
Example output:
path/to/file:1: This is a line containing the search_pattern.
path/to/file:5: Another line with the search_pattern.
Use case 2: Search for extended regular expressions in a compressed file (case-insensitive)
Code:
zegrep --ignore-case "search_pattern" path/to/file
Motivation: This use case is helpful when you want to search for extended regular expressions in a compressed file, without considering the case sensitivity of the search.
Explanation:
--ignore-case
: This option tells zegrep to perform a case-insensitive search."search_pattern"
: This is the pattern you want to search for in the file.path/to/file
: This is the path of the compressed file you want to search in.
Example output:
path/to/file:1: This is a line containing the search_pattern.
path/to/file:3: Another line with the SEARCH_PATTERN.
Use case 3: Search for lines that do not match a pattern
Code:
zegrep --invert-match "search_pattern" path/to/file
Motivation: This use case is useful when you want to find lines in a compressed file that do not match a specific pattern.
Explanation:
--invert-match
: This option tells zegrep to print lines that do not match the specified pattern."search_pattern"
: This is the pattern you want to search for in the file.path/to/file
: This is the path of the compressed file you want to search in.
Example output:
path/to/file:2: This line does not match the search_pattern.
path/to/file:4: Another line without the search_pattern.
Use case 4: Print file name and line number for each match
Code:
zegrep --with-filename --line-number "search_pattern" path/to/file
Motivation: This use case is helpful when you want to know the file name and line number for each match of a pattern in a compressed file.
Explanation:
--with-filename
: This option tells zegrep to print the file name before each match.--line-number
: This option tells zegrep to print the line number for each match."search_pattern"
: This is the pattern you want to search for in the file.path/to/file
: This is the path of the compressed file you want to search in.
Example output:
path/to/file:1: This is a line containing the search_pattern.
Use case 5: Search for lines matching a pattern, printing only the matched text
Code:
zegrep --only-matching "search_pattern" path/to/file
Motivation: This use case is useful when you want to search for a pattern in a compressed file and only need the actual matched text, without the rest of the line.
Explanation:
--only-matching
: This option tells zegrep to print only the matched text, without the rest of the line."search_pattern"
: This is the pattern you want to search for in the file.path/to/file
: This is the path of the compressed file you want to search in.
Example output:
search_pattern
SEARCH_PATTERN
Use case 6: Recursively search files in a compressed file for a pattern
Code:
zegrep --recursive "search_pattern" path/to/file
Motivation: This use case is helpful when you want to recursively search for a pattern within files contained in a compressed file.
Explanation:
--recursive
: This option tells zegrep to search for the pattern recursively within files."search_pattern"
: This is the pattern you want to search for in the file.path/to/file
: This is the path of the compressed file you want to search in.
Example output:
path/to/file:1: This is a line containing the search_pattern.
path/to/file/subfile:3: Another line with the search_pattern.
path/to/file/subfolder/another_file:2: Yet another match for search_pattern.
Conclusion:
The zegrep command provides a convenient way to search for extended regular expression patterns in compressed files. Whether it’s a case-sensitive or case-insensitive search, printing file names and line numbers, matching only the text of a pattern, or recursively searching within files, zegrep offers various options to meet your search requirements.