Skip to main content

A command line tool to apply substitutions to a text file

Project description

apply-subs

PyPI PyPI codecov pre-commit.ci status Imports: isort Code style: black

A command line tool to apply a dictionnary (json) of substitutions to text file corpus. This program is a find-and-replace tool to perform a arbitrarily large set of substitutions in a reproducible fashion.

Installation

The easiest installation method is

$ pip install apply-subs

In order to install apply-subs in isolation, use pipx instead.

Examples

minimal case

$ echo "Lorem ipsum dolor sit amet, consectetur adipiscing elit" > mytext.txt
$ echo '{"Hello": "Lorem ipsum", "goodbye": "adipiscing elit"}' > mysubs.json
$ apply-subs mytext.txt -s mysubs.json

will print the patched content

Hello dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore goodbye.

diff mode

Use diff mode (-d/--diff) to print a diff instead of the end result

--- mytext.txt
+++ mytext.txt (patched)
@@ -1 +1 @@
-Lorem ipsum dolor sit amet, consectetur adipiscing elit
+Hello dolor sit amet, consectetur goodbye

Use -cp/--cdiff/--colored-diff for a colored output (when supported).

inplace substitutions

-i/--inplace

$ apply-subs --inplace mytext.txt -s mysubs.json

is equivalent to

$ apply-subs mytext.txt -s mysubs.json > mytext.txt

target several files in one go

The target positional argument can consist of a single file (as illustrated above), or many. This is useful for instance if you need to apply a set of subtitutions to all files in a project whose name match a regexp.

$ git ls-files | egrep "(.md|.py)$" | xargs apply-subs -s subsubs.json -i

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

apply_subs-0.5.3.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

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

apply_subs-0.5.3-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

Details for the file apply_subs-0.5.3.tar.gz.

File metadata

  • Download URL: apply_subs-0.5.3.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.9.1

File hashes

Hashes for apply_subs-0.5.3.tar.gz
Algorithm Hash digest
SHA256 873996c97009635d1a30a351651b485bfa113a4448ca3d948e893ec3076d66b5
MD5 c26215665f7381f5b0f8c285c9e89b93
BLAKE2b-256 97ccc07ec826acbc4e950eb3fe1a4ccbd95edc8bdeddf541ef09349046de91ea

See more details on using hashes here.

File details

Details for the file apply_subs-0.5.3-py3-none-any.whl.

File metadata

  • Download URL: apply_subs-0.5.3-py3-none-any.whl
  • Upload date:
  • Size: 16.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.9.1

File hashes

Hashes for apply_subs-0.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 10eaf7d8ba2c800f2571de089202da3117057f1c3eddc30e6c23c501271dc644
MD5 f72e60fb88a5ef8846eabb73049e6107
BLAKE2b-256 fe11586dcd2a0e4da5191a90d9754cb758da4048d0c2edf58946c5029969cb24

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