LinuxCommandLibrary

bzgrep

Search compressed bzip2 files for a pattern

TLDR

Search for a pattern within a compressed file

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

Use extended regular expressions (supports ?, +, {}, () and |), in case-insensitive mode
$ bzgrep --extended-regexp --ignore-case "[search_pattern]" [path/to/file]
copy

Print 3 lines of context around, before, or after each match
$ bzgrep --[context|before-context|after-context]=[3] "[search_pattern]" [path/to/file]
copy

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

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

Recursively search files in a bzip2 compressed tar archive for a pattern
$ bzgrep --recursive "[search_pattern]" [path/to/tar/file]
copy

Search stdin for lines that do not match a pattern
$ cat [/path/to/bz/compressed/file] | bzgrep --invert-match "[search_pattern]"
copy

SYNOPSIS

bzgrep [grep_options] pattern [file1 [file2 ...]]

PARAMETERS

grep_options
    Any valid options supported by the grep command, such as -i (ignore case), -n (line numbers), -v (invert match), -r (recursive search), etc.

pattern
    The regular expression to search for within the bzip2-compressed file(s).

file1 [file2 ...]
    One or more bzip2-compressed (.bz2) or regular files to search. If no files are specified, bzgrep reads from standard input.

DESCRIPTION

The bzgrep command is a utility in Linux used to search for a regular expression in bzip2-compressed files. It combines the functionalities of bzcat (which decompresses bzip2 files) and grep (which searches for patterns). Rather than explicitly decompressing a .bz2 file and then using grep, bzgrep performs both operations in a single step, simplifying the search process and saving disk space by avoiding the need for temporary uncompressed files.

It can also search uncompressed files using regular grep. The command uses either the bunzip2 command with the -c flag (or bzip2 -dc) to decompress the file. This output is then piped directly to the grep command for pattern matching. The default command path for bunzip2 is searched for. The program exits with a status of 0 if any lines are selected, 1 if no lines were selected, and 2 if an error occurred. bzgrep is part of the bzip2 package, so ensure it is installed on your system.

Because bzgrep leverages grep, it supports all the standard grep options for controlling the search behavior, output format, and matching criteria. This makes it a versatile tool for examining the contents of compressed logs, configuration files, and other bzip2-compressed data.

CAVEATS

bzgrep relies on the availability of bunzip2 (or bzip2) and grep in the system's PATH. Ensure these utilities are installed and accessible.

EXIT STATUS

The exit status of bzgrep is identical to that of grep. Exit status 0 means one or more lines were selected, 1 means no lines were selected, and 2 indicates an error occured.

ENVIRONMENT

The environment variable GREP specifies the default options to be passed to the grep command. For example, setting GREP='-i' would cause grep to ignore case by default.

HISTORY

bzgrep was created to provide a convenient way to search compressed files directly, avoiding the need for manual decompression. Its functionality mirrors that of zgrep for gzip files, extending similar search capabilities to bzip2 compressed data.

SEE ALSO

bzcat(1), grep(1), bzip2(1), bunzip2(1), zgrep(1), xzgrep(1)

Copied to clipboard