Skip to main content

marcgrep PyPI

A CLI for searching MARC files like MARCgrep.pl but in Python and a bit different syntax.

marcli is also a similar project that's faster but a little less flexible.

Installation

Python 3.10 or later.

pipx install marcgrep # install globally with pipx
pip install marcgrep # or use pip/pip3

Usage

# general command format - pass one or more files or pipe stdin
marcgrep OPTIONS FILE1.mrc FILE2.mrc
cat FILE.mrc | marcgrep OPTIONS
# full usage information
Usage: marcgrep [OPTIONS] [FILES]...

  Find MARC records matching patterns in a file.

Options:
  -h, --help           Show this message and exit.
  -c, --count          Count matching records
  -i, --include TEXT   Include matching records (repeatable)
  -e, --exclude TEXT   Exclude matching records (repeatable)
  -f, --fields TEXT    Comma-separated list of fields to print
  -l, --limit INTEGER  Limit number of records to process
  --color              Colorize mnemonic MARC output
  --invert             Invert color scheme (for light terminal backgrounds)
  --version            Show the version and exit.

The --include and --exclude flags can be used multiple times to specify multiple criteria. They accept a pattern which is a sort of comma-separated filter expression for matching MARC fields. Examples:

# records with a 780 field
marcgrep -i 780 FILE.mrc
# records with Ulysses in the 245 field
marcgrep -i '245,Ulysses' FILE.mrc
# titles _without_ "Collected Poems" in the 245 ‡a subfield
marcgrep -e '245,a,Collected Poems' FILE.mrc
# titles with second indicator = 4 that do not start with "The "
marcgrep -i '245,,4,,^(?!The )' FILE.mrc

The meaning of the filter expression's components depends upon their number:

  • 1: field, 910 -> 910 is in record
  • 2: field and value (regular expression), 100,Lorde -> 100 contains string "Lorde"
  • 3: field, subfield, and value, 506,a,Open Access -> 506‡a contains string "Open Access"
  • 4: field, subfield, first indicator, and value, 856,0,u,@lcsh\.gov -> 856‡u with 1st indicator 0 contains string "@lcsh.gov"
  • 5: field, subfield, first & second indicators, and value, 245,0,4,a,The Communist Manifesto

The intention of this syntax is to facilitate searching subfields and field values more easily than MARCgrep.pl since we care about them more often than indicators. To ignore a component but use one of lesser priority, leave the component empty. For instance, 856,s, refers to records with an 856 field with an s subfield but the trailing comma means we don't care about the subfield's value. The pattern 245,,4,, refers to records with a 245 field with a second indicator of 4 regardless its subfields or value.

To use a literal comma in a value pattern, include all the other components. For instance, to search for "Morrison, Toni" anywhere in a 100 field, use 100,,,,Morrison, Toni.

Multiple criteria are combined with logical AND. Multiple --include flags is narrower than one, as is an --include and an --exclude.

Color & Formatting

The --color flag lets you pick colors for various parts of a MARC record using environment variables. You can pick from the available termcolor colors. The defaults are:

Component Color Var
Tag cyan MARC_TAG_COLOR
Indicator light_yellow MARC_INDICATOR_COLOR
Subfield code green MARC_SUBFIELD_COLOR
Data white MARC_DATA_COLOR

There is an inverted color scheme available with the --invert flag for use with light (e.g. white) terminal backgrounds.

You can also configure the subfield delimiter character and the symbol for an empty indicator. Those defaults are:

Symbol Var
‡ MARC_SUBFIELD_DELIMITER
_ MARC_EMPTY_INDICATOR

Development

uv is used for development.

uv sync # install dependencies
uv run pytest # run tests
uv run ruff format . # format code
uv run ruff check . # lint code
uv build # build package, used in CI

Any tag triggers a release to Test PyPI. Any tag beginning with the letter v requires manual approval to be released to PyPI and GitHub. Remember to increment the version in pyproject.toml or the PYPI publish will fail. There are protection rules on the pypi and testpypi environments to this effect, too.

License

MIT © Eric Phetteplace 2024.

Metadata

Release files for marcgrep 2.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for marcgrep 2.0.2
File Size Uploaded Provenance Actions
marcgrep-2.0.2.tar.gz 8.5 kB With provenance Details for marcgrep-2.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for marcgrep 2.0.2
File Interpreter ABI Platform Actions
marcgrep-2.0.2-py3-none-any.whl Python 3 none any Details for marcgrep-2.0.2-py3-none-any.whl

Total release size: 16.1 kB

Release files / marcgrep-2.0.2.tar.gz

Download URL marcgrep-2.0.2.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
cc3c0e5e461bb26f5be8b53338fd58e14d1c3337e7abdfaa0983e0ef7483d34b
BLAKE2b-256 checksum
How to use checksums
c2cf7e3b67890e4cd3f83d407f78c0a34370641e1cdbdac33cc24b57c5734086
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.

Transparency log

Release files / marcgrep-2.0.2-py3-none-any.whl

Download URL marcgrep-2.0.2-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b83e7b8abdf5f2365ddf8839006d6d89b82dd1718556de8f7a40f90848ddbfae
BLAKE2b-256 checksum
How to use checksums
fc06e8f804d156643c75af77f9d3ff9da6abfbd7ed3b86d46a6297c824512c09
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.

Transparency log

Security for marcgrep 2.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for marcgrep-2.0.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 2, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page