LinuxCommandLibrary

zegrep

Search compressed files for a pattern

TLDR

Search for extended regular expressions (supporting ?, +, {}, () and |) in a compressed file (case-sensitive)

$ zegrep "[search_pattern]" [path/to/file]
copy

Search for extended regular expressions (supporting ?, +, {}, () and |) in a compressed file (case-insensitive)
$ zegrep [[-i|--ignore-case]] "[search_pattern]" [path/to/file]
copy

Search for lines that do not match a pattern
$ zegrep [[-v|--invert-match]] "[search_pattern]" [path/to/file]
copy

Print file name and line number for each match
$ zegrep [[-H|--with-filename]] [[-n|--line-number]] "[search_pattern]" [path/to/file]
copy

Search for lines matching a pattern, printing only the matched text
$ zegrep [[-o|--only-matching]] "[search_pattern]" [path/to/file]
copy

Recursively search files in a compressed file for a pattern
$ zegrep [[-r|--recursive]] "[search_pattern]" [path/to/file]
copy

SYNOPSIS

zegrep [options] pattern [file1 file2 ...]

PARAMETERS

-E, --extended-regexp
    Interpret PATTERN as an extended regular expression (ERE).

-F, --fixed-strings
    Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.

-G, --basic-regexp
    Interpret PATTERN as a basic regular expression (BRE). This is the default.

-P, --perl-regexp
    Interpret PATTERN as a Perl-compatible regular expression (PCRE).

-e PATTERN, --regexp=PATTERN
    Use PATTERN as the pattern. Multiple -e options can be used to specify multiple patterns.

-f FILE, --file=FILE
    Obtain PATTERN from FILE. One pattern per line.

-i, --ignore-case
    Ignore case distinctions in both the PATTERN and the input files.

-v, --invert-match
    Select non-matching lines.

-w, --word-regexp
    Select only those lines containing matches that form whole words.

-x, --line-regexp
    Select only those matches that exactly match the whole line.

-c, --count
    Print only a count of matching lines for each input file.

-n, --line-number
    Prefix each line of output with the line number within its input file.

-b, --byte-offset
    Prefix each line of output with the byte offset within its input file.

-H, --with-filename
    Print the file name for each match.

-h, --no-filename
    Suppress the prefixing of file names on output.

-l, --files-with-matches
    Print only the names of files containing matches, one per line.

-L, --files-without-match
    Print only the names of files containing no matches, one per line.

-o, --only-matching
    Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

-q, --quiet, --silent
    Suppress all normal output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the -s or --no-messages option.

-s, --no-messages
    Suppress error messages about nonexistent or unreadable files.

-r, --recursive
    Read all files under each directory, recursively; this is equivalent to the -d recurse option.

-d ACTION, --directories=ACTION
    How to handle directories. ACTION may be 'read', 'recurse', or 'skip'.

-D ACTION, --devices=ACTION
    How to handle devices, FIFOs and sockets. ACTION may be 'read' or 'skip'.

-R, --dereference-recursive
    Read all files under each directory, recursively, following all symbolic links. Equivalent to -r --dereference.

-m NUM, --max-count=NUM
    Stop reading a file after NUM matching lines.

-B NUM, --before-context=NUM
    Print NUM lines of leading context before matching lines.

-A NUM, --after-context=NUM
    Print NUM lines of trailing context after matching lines.

-C NUM, --context=NUM
    Print NUM lines of output context.

--color[=WHEN], --colour[=WHEN]
    Surround the matched (non-empty) strings, matching lines, context lines, file names, line numbers, byte offsets, and separators (for fields and groups of context lines) with escape sequences to display them in color on the terminal. The WHEN argument is never, always, or auto.

-U, --binary
    Treat the file(s) as binary. By default, under MS-DOS and MS-Windows, grep guesses whether a file is text or binary as described for the --binary-files option. If grep decides the file is a text file, it strips carriage returns from the original file. Specifying -U tells grep to desist from this guessing, and read and print the file verbatim. This will work fine if the file is text; if it is garbage binary, it will only make more sense to display such garbage.

-z, --null-data
    Treat the input as a set of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. Like the -Z or --null option in commands like sort. Useful when input comes from commands like find -print0.

DESCRIPTION

The `zegrep` command is used to search potentially compressed files for lines matching a pattern. It is effectively a wrapper around `zgrep`, which itself combines `grep` and a decompression utility like `gzip`, `bzip2`, or `xz`. `zegrep` automatically detects the compression type (gzip, bzip2, lzma, xz, etc.) and decompresses the file before passing it to `grep`. This allows users to search for text within compressed archives without explicitly decompressing them first. It's especially useful when dealing with log files or other large text files that are frequently compressed to save disk space.

Key Advantages:
1. Simplifies searching compressed files.
2. Automatically handles various compression formats.
3. Reduces the need for manual decompression.

In most modern Linux systems `zgrep` and implicitly `zegrep` are symlinks to `grep -z`. This means that `grep` automatically handles compressed files when the `-z` option is specified. It simplifies the process by directly incorporating decompression during the search, which makes `zegrep` a convenient and frequently used command in environments where dealing with archived data is commonplace.

CAVEATS

The actual implementation and features available may vary slightly depending on the version of `grep` and the system's configuration.

EXIT STATUS

The `zegrep` command exits with status 0 if at least one line is selected, 1 if no lines are selected, and 2 if an error occurred.

ENVIRONMENT VARIABLES

GREP_OPTIONS:
Specifies default options to be placed in front of any explicit options. For example, 'GREP_OPTIONS='--binary-files=without-match --directories=skip'' sets a default behavior. Options in GREP_OPTIONS are overridden by explicit options.
GREP_COLOR:
Specifies the color used to highlight matched text. Defaults to 'ms=01;31' (bold red).

HISTORY

The `zgrep` command emerged as a utility to streamline searching within compressed files. Initially, users had to manually decompress files using tools like `gzip` or `bzip2` before using `grep`. `zgrep` automated this process, making it much more efficient to search within archived data. Over time, its functionality has been integrated into `grep` itself via the `-z` option, making `zgrep` often implemented as a symbolic link to `grep -z`.

SEE ALSO

grep(1), zcat(1), gzip(1), bzip2(1), xz(1)

Copied to clipboard