Skip to main content

xgrep: A grep for Excel (and CSV/TSV) files

Project description

xgrep

Documentation

Can be found at https://xgrep.readthedocs.io

Example usage

To give an example of using xgrep, suppose you have an Excel file with a sheet that looks like this:

You'll find this file here if you want to try the following commands.

Find cells based on a regular expression

Look for the regular expression 'Xia|radius|Jilin':

$ xgrep 'Xia|radius|Jilin' docs/source/example.xlsx

Note that the command-line output is all text. It is produced using the Table class of the wonderful rich package.

Display the row and column information

Add the row numbers (--rn) and Excel column (--ec) information from the input Excel:

$ xgrep --rn --ec 'Xia|radius|Jilin' docs/source/example.xlsx

De-emphasize unmatched cells

If you don't care about the values in cells that were not matched, you can give a value to display instead:

$ xgrep -u . --rn --ec 'Xia|radius|Jilin' docs/source/example.xlsx

Only show matching columns

To exclude columns with no matching cells, you can show only matching columns (--omc):

$ xgrep --omc --rn --ec 'Xia|radius|Jilin' docs/source/example.xlsx

Write CSV (or TSV)

The default output format is a rich Table. You can also produce CSV, TSV, or Excel using the --format option:

$ xgrep --format csv 'Xia|radius|Jilin' docs/source/example.xlsx

If you use --format excel you will also need to give an output filename using --out.

Usage

$ xgrep --help
Usage: xgrep [OPTIONS] PATTERN FILENAMES...

  Command-line interface.

Options:
  -o, --out FILE                  The output file. If not given, output is
                                  written to standard out. Note that if the
                                  --quiet (-q) options is given, no output is
                                  written to the output file.
  --header / --no-header          Don't look for a header line. In this case,
                                  text in what would otherwise be considered a
                                  header can also be matched by the grep
                                  pattern.
  --skip INTEGER RANGE            Skip this many rows at the start of the
                                  input file(s).  [x>=0]
  --format [csv|excel|rich|tsv]   The output format.
  -c, --count                     Only print the number of matching lines.
  --width INTEGER                 The width to use for --format rich tables.
  -v, --invert                    Print non-matching lines.
  -q, --quiet                     Do not show any output, just exit with a
                                  status indiacting whether a match was found
                                  (0) or not (1).
  --only-matching-cols, --omc, --mco
                                  Only show columns that have a matching cell.
  -i, --ignore-case               Ignore case while matching.
  --color TEXT                    The highlight color.
  -u, --unmatched TEXT            The string to show for cells whose values do
                                  not match. If not given, non-matching cells
                                  are shown with their value (in which case
                                  you will need to use the output color to see
                                  matches).
  --row-numbers, --rn             Show row numbers.
  --col-numbers, --cn             Show column numbers.
  --excel-cols, --ec              Add Excel column labels to column names.
  Filenames: [mutually_exclusive]
                                  Whether to display names of matching files.
    -H, --filenames-always, --fa  Always print the name of matching files
                                  (like grep -H).
    -h, --no-filename, --nf       Never print the name of matching files (like
                                  grep -h).
    --only-filename, --of         Only print the names of matching files, not
                                  their matched content.
  --help                          Show this message and exit.

Todo

  1. Make it possible to specify a sheet (only works if there is one Excel file?).
  2. Adjust col names Row and File in case they're already present.
  3. Allow for processing multiple Excel files into individual sheets.
  4. Write tests for unequal numbers of cols.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xgrep-0.2.3.tar.gz (4.5 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xgrep-0.2.3-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file xgrep-0.2.3.tar.gz.

File metadata

  • Download URL: xgrep-0.2.3.tar.gz
  • Upload date:
  • Size: 4.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.11

File hashes

Hashes for xgrep-0.2.3.tar.gz
Algorithm Hash digest
SHA256 36d71cf2a91e9d60b6c59313a8246d424634a6383390a493d800fd74615fa6a3
MD5 00154b45eb94c152c0fb260eadbe2ed8
BLAKE2b-256 b5f116dd740db4a22d28fd0db5478700e03026b00f58158de417d20df3cfc7c5

See more details on using hashes here.

File details

Details for the file xgrep-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: xgrep-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.11

File hashes

Hashes for xgrep-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 412307655cebd3845ac11ef5e5ff5ba6337f5d6f6d7f426b8cba270165e84954
MD5 05cd2a0bcb1a6a90ee082cdf0e1d827e
BLAKE2b-256 5ea62c9787a2eb3f19ee546a573874991c6781c573a472fb75bdff6b6fa2e26b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page