LinuxCommandLibrary

xzegrep

Search compressed files for regular expressions

TLDR

View documentation for the original command

$ tldr xzgrep
copy

SYNOPSIS

xzegrep [options] pattern [file ...]

PARAMETERS

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

-v, --invert-match
    
Invert the sense of matching, to select non-matching lines.

-c, --count
    
Suppress normal output; instead print a count of matching lines for each input file.

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

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

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

-C NUM, --context=NUM
    
Print NUM lines of leading and trailing context. Equals -A NUM -B NUM.

-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.

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

-l, --files-with-matches
    
Suppress normal output; instead print the name of each input file from which output would normally have been printed.

-L, --files-without-match
    
Suppress normal output; instead print the name of each input file from which no output would normally have been printed.

-q, --quiet, --silent
    
Suppress all normal output. Exit immediately with zero status if any match is found, one if no match is found, or two for errors.

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

-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.

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

-r, --recursive
    
Read all files under each directory, recursively. This option is equivalent to -R.

--exclude=GLOB
    
Skip files whose base name matches GLOB (a wildcard pattern).

--include=GLOB
    
Search only files whose base name matches GLOB.

--exclude-dir=DIR
    
Exclude directories matching the pattern DIR from recursive searches.

DESCRIPTION

xzegrep is a convenient command-line utility designed to search for patterns within compressed files (e.g., *.gz, *.bz2, *.xz) and present the output in an interactive pager. It typically acts as a wrapper around the zgrep command, piping its output to a pager like less or more. This integration allows users to efficiently navigate through potentially large search results from compressed log files or data archives, providing features like scrolling, searching within results, and highlighting. While zgrep itself performs the pattern matching on compressed data, xzegrep enhances the user experience by making the output more manageable and browsable, particularly when the search yields numerous lines. Its primary benefit lies in combining the powerful searching capabilities of grep (via zgrep) with the interactive viewing features of a pager, making it an invaluable tool for system administrators and developers analyzing compressed data.

CAVEATS

xzegrep is often not a standalone binary command but rather a shell script or an alias that combines zgrep with a pager program (most commonly less). As such, its exact behavior and availability can vary between different Linux distributions or user environments. If xzegrep is not found, users can typically create their own version (e.g., alias xzegrep='zgrep "$@" | less -R') to achieve the same functionality. The interactive features and keybindings depend entirely on the pager used (e.g., less).

CREATING YOUR OWN <I>XZEGREP</I>

If xzegrep is not pre-installed on your system, you can easily create an alias or a shell script to replicate its functionality. A common approach is:
alias xzegrep='zgrep "$@" | less -R'
or a script named xzegrep in your PATH:

#!/bin/sh
zgrep "$@" | less -R

The -R option passed to less is crucial as it tells less to output "raw" control characters, which ensures that grep's colorized output (if enabled) is correctly displayed.

INTERACTING WITH THE PAGER

Once xzegrep displays the output, you are interacting with the pager (e.g., less). Common commands within less include:
q: Quit
spacebar: Scroll down a full screen
b: Scroll up a full screen
/pattern: Search forward for a pattern
?pattern: Search backward for a pattern
n: Go to the next match of the last search
N: Go to the previous match of the last search.

HISTORY

The concept of xzegrep emerged from the need to easily view the often voluminous output of searching compressed files. While zgrep effectively handles the decompression and searching, its raw output can be overwhelming for large datasets. Users and system administrators naturally started piping zgrep's output into pagers like less for better readability and navigability. xzegrep is essentially a formalized or aliased version of this common workflow, providing a convenient shortcut. It doesn't have a distinct development history as a standalone project but rather represents a best practice and common utility script evolved from the practical usage of zgrep and interactive pagers in Unix-like environments.

SEE ALSO

zgrep(1), grep(1), less(1), more(1)

Copied to clipboard