bzgrep
Search compressed bzip2 files for a pattern
TLDR
Search for a pattern within a compressed file
Use extended regex (supports ?, +, {}, () and |), in case-insensitive mode
Print 3 lines of [C]ontext around, [B]efore, or [A]fter each match
Print file name and line number for each match
Search for lines matching a pattern, printing only the matched text
Recursively search files in a bzip2 compressed tar archive for a pattern
Search stdin for lines that do not match a pattern
SYNOPSIS
bzgrep [OPTIONS] PATTERN [FILE...]
Explanation:
OPTIONS: Any valid grep options that control the search behavior.
PATTERN: The regular expression or fixed string to search for.
FILE...: One or more bzip2 compressed files (or standard uncompressed files) to search. If no files are specified, bzgrep reads from standard input.
PARAMETERS
-i, --ignore-case
Ignore case distinctions in both the PATTERN and the input files.
-v, --invert-match
Invert the sense of matching, to select non-matching lines.
-r, --recursive
Recursively search subdirectories listed.
-l, --files-with-matches
Suppress normal output; instead print the name of each input file from which output would normally have been printed.
-n, --line-number
Prefix each line of output with the 1-based line number within its input file.
-c, --count
Suppress normal output; instead print a count of matching lines for each input file.
-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.
-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 output context around matching lines.
--help
Display a help message and exit.
DESCRIPTION
bzgrep is a powerful command-line utility that allows users to search for patterns within files compressed with bzip2. It functions similarly to the standard grep command, but with the added convenience of automatically decompressing .bz2 files on the fly before searching. This eliminates the need for users to manually decompress files using bzip2 -d or bunzip2 before running a grep search.
Essentially, bzgrep acts as a wrapper script around grep and bzip2. When invoked, it pipes the decompressed output of the bzip2 files directly into grep, allowing all standard grep options and functionalities to be utilized. This makes it an indispensable tool for system administrators and developers working with compressed log files, archives, or any data stored in the bzip2 format, saving time and simplifying workflows.
CAVEATS
Performance: For very large bzip2 files, bzgrep might be slower than decompressing the file once and then running grep, as it decompresses the file for every invocation.
Dependencies: It relies on both bzip2 and grep being installed and accessible in the system's PATH.
Error Handling: As a wrapper script, its error messages might sometimes be less direct than those from the underlying grep or bzip2 commands.
HOW IT WORKS INTERNALLY
bzgrep is typically a shell script that orchestrates the decompression and searching process. It usually works by piping the output of bzip2 -dc (decompress to standard output) into grep. For example, a command like bzgrep PATTERN file.bz2
might internally execute something similar to bzip2 -dc file.bz2 | grep PATTERN
. This allows it to pass all grep-specific options directly to the grep command, maintaining its full functionality.
HISTORY
bzgrep is part of the bzip2 suite of utilities, which were developed by Julian Seward. The bzip2 compression algorithm itself was created in 1996, and the associated utilities like bzgrep were developed to provide seamless interaction with bzip2 compressed files, mirroring the functionality provided by zgrep for gzip compressed files. Its development focused on providing convenience for users working with bzip2 data, integrating directly into common command-line workflows.