Skip to main content

Pyreutil is a command line utility to bulk edit plain text files or filenames using regex.

Project description

pyreutil

Pyreutil is a simple command line utiliy to bulk edit filenames or text files using regex.

Getting started

You can install the package using pip by running the following command:

$ pip install pyreutil

Or for manual installation using the CLI

$ git clone https://github.com/michsun/pyreutil.git
$ cd pyreutil
$ python setup.py install

Usage

If installed using pip or CLI, you can run the utility using the following command structure.

$ pyreutil [-f or -t] [path_to_directory_or_file] [arguments...]

Otherwise, if downloaded directly, go to the pyreutil folder and use the following commands:

$ python -m pyreutil [-f or -t] [path_to_directory_or_file] [arguments...]
OR
$ python3 -m pyreutil [-f or -t] [path_to_directory_or_file] [arguments...]

Commands

The full list of commands is listed below:

usage: pyreutil [-h] [-t TEXTFILES | -f FILENAMES] [-a APPEND] [-af APPEND_FILE] [-c] [-d] [-i]
                [-l LAMBDA_FUNC] [-r REPLACE] [-rf REPLACE_FILE] [-g GROUP] [-rm REMOVE] [-s SEARCH]
                [-si] [-w] [-md]

pyreutil - A python command line utility for searching and modifying files and filenames using regex.

optional arguments:
  -a APPEND, --append APPEND
                        text to append
  -af APPEND_FILE, --append-file APPEND_FILE
                        appends file contents
  -c, --copy            saves changes as a copy of the original directory/file
  -d, --deep            search subdirectories if a directory is given
  -f FILENAMES, --filenames FILENAMES
                        filenames source
  -g GROUP, --group GROUP
                        integer representing the group to replace. Must be used with -s --search
  -h, --help            show this help message and exit
  -i, --inplace         save changes to the existing directory/file
  -l LAMBDA_FUNC, --lambda-func LAMBDA_FUNC
                        code string to execute in a lambda function
  -rm REMOVE, --remove REMOVE
                        removes regex matches
  -md, --remove-md-links
                        removes markdown links and replaces it with the link name
  -w, --remove-whitespaces
                        removes redundant whitespaces (repeat, leading, trailing, and spaces before a
                        period or comma)
  -r REPLACE, --replace REPLACE
                        string to replace searches with. Must be used with -s --search
  -rf REPLACE_FILE, --replace-file REPLACE_FILE
                        file containing contents to replaces regex searches with. Must be used with -s
                        --search
  -s SEARCH, --search SEARCH
                        searches for regex matches
  -si, --silence        silences the output
  -t TEXTFILES, --textfiles TEXTFILES
                        text source

If functions are used in conjunction, they are processed in the following order:

  1. Remove
  2. Append/Append file contents
  3. Search and replace with string/file contents/group/lambda function.
  4. Remove redundant whitespaces

Examples

  • Search and replace - basic

To search and replace with a string, you can use the following command

$ pyreutil -f examples/swift -s '-' -r '_' -i

Basic search and replace example

  • Search and replace - group

To search and replace with a regex group, you can use the following command:

$ pyreutil -t examples/markdown/game-theory-wiki.md -s "\[([\[]?[^\[^\]]+[\]]?)]\((http[s]?://[^\)]+)\)" -g=1

Search and replace with regex groups

Alternatively, you can use group capture syntax e.g. -r='\g<1>' or -r=\1, both of which function the same as -g=1.

  • Search and replace - lambda

You can also use lambda function syntax to make regex substitutes with the -l or --lambda-func arguments as in the following example.

$ pyreutil -f examples/swift -s "(_)([a-z])" -l "x.group(2).upper()"

Search and replace - lambda function example

Your code should work in the following function: re.sub(search_str, lambda x : <lambda_code> , text).

  • Saving changes

For any modifications, only a preview is shown by default. To save the changes, you can use -i to save inplace, or -c to save the changes as a copy of the original file.

When using -c, if a single file is given, a copy of the file is made in the original destination. Alternatively, if a directory is given, a copy of the original folder is made with all files copied. Note that all files are included, even files that don't have any regex matches. When used in conjunction with -d or --deep, the same is applied to all files in subdirectories.

An example using -d and -c is shown below.

$ pyreutil -f examples/swift -s "(_)([a-z])" -l "x.group(2).upper()" -dc

Search and replace filename with -c and -d example

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

pyreutil-0.0.3.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

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

pyreutil-0.0.3-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file pyreutil-0.0.3.tar.gz.

File metadata

  • Download URL: pyreutil-0.0.3.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for pyreutil-0.0.3.tar.gz
Algorithm Hash digest
SHA256 2984944baed9ddce0275d8d78e1fd2ea0130a51514914190a77151cc488c9218
MD5 ae7ee13a88a1f66292e9c13aa2ce31c4
BLAKE2b-256 20390af0bb6478d1d0376b8c27e0ade4c45907a3a86aecf90e96976c7ba6c533

See more details on using hashes here.

File details

Details for the file pyreutil-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: pyreutil-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for pyreutil-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 208638c2e662ab58d5c903bc5e006b1f3c0f5a46f603a1360eb54b79ac8ba2d3
MD5 5581c8cd774349dba0f0d207778cb9a6
BLAKE2b-256 08690e6532ab54d08e27263d22df7d820f6fb9e4ce6ec5bae5b4006f63b199d3

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