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:
- Remove
- Append/Append file contents
- Search and replace with string/file contents/group/lambda function.
- 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
- 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
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()"
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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyreutil-0.0.4.tar.gz.
File metadata
- Download URL: pyreutil-0.0.4.tar.gz
- Upload date:
- Size: 12.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bf694d65f8b8c07f2a87d66996b89f4226257a27f84097b1fce1fec4af230d48
|
|
| MD5 |
96d56d8eee933ea8c3538ad82b030806
|
|
| BLAKE2b-256 |
204d17ac5d5e1153ad476f264483ceeac465c08748622bd9ca4a5609d3a2ea92
|
File details
Details for the file pyreutil-0.0.4-py3-none-any.whl.
File metadata
- Download URL: pyreutil-0.0.4-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
45a0f34ff1fc3b86cd729fdbeaa3f01bb634d45658e67a134ee2ca0613724a47
|
|
| MD5 |
df28d68fab12613697a7cf23e11bfb0d
|
|
| BLAKE2b-256 |
083058b1b7eb5049eddaf52ecdd42db53918b583c02765f274978b453c0a36b0
|