Skip to main content

`subst` is simple utility to replace one string into another in given list of files.

Project description

subst is simple utility to replace one string into another in given list of files.

Current stable version

0.4.0

But why?

  1. There is sed for example?

    Yes, it is. But sed use regexps engine called “Basic Regular Expressions”, or “Extended Regular Expression”. PCRE is much more widely used dialect.

  2. So I can use Perl!

    Of course you can. But not everyone know how to use Perl. I know, but subst is IMHO simpler to use.

OK, so how to use it?

Simple

echo 'Hello World!' | subst -s 's/Hello/Hi/' -

or:

subst -p '(192\.168)\.1\.(10)' -r '\1.0.\2' /etc/hosts

More

Everything is in help :) Just execute:

subst --help

Look at result:

% subst --help
usage: subst.py [-h] [-p PATTERN] [-r REPLACE] [--eval-replace] [-t STRING]
                [-s "s/PAT/REP/gixsm"] [-c COUNT] [-l] [-i]
                [--pattern-dot-all] [--pattern-verbose] [--pattern-multiline]
                [--utf8] [--encoding-input ENCODING_INPUT]
                [--encoding-file ENCODING_FILE]
                [--encoding-filesystem ENCODING_FILESYSTEM] [-b] [-e EXT]
                [--stdin] [--stdout] [--verbose] [--debug] [-v]
                [files [files ...]]

Replace PATTERN with REPLACE in many files.

positional arguments:
  files                 file to parse.

optional arguments:
  -h, --help            show this help message and exit
  -p PATTERN, --pattern PATTERN
                        pattern to replace for. Supersede --pattern-and-
                        replace. Required if --replace is specified.
  -r REPLACE, --replace REPLACE
                        replacement. Supersede --pattern-and-replace. Required
                        if --pattern is specified.
  --eval-replace        if specified, make eval data from --replace(should be
                        valid Python code). Ignored with --pattern-and-replace
                        argument.
  -t STRING, --string STRING
                        if specified, treats --pattern as string, not as
                        regular expression. Ignored with --pattern-and-replace
                        argument.
  -s "s/PAT/REP/gixsm", --pattern-and-replace "s/PAT/REP/gixsm", --pattern-and-replace "s/PAT/REP/gixsm"
                        pattern and replacement in one:
                        s/pattern/replace/g(pattern is always regular
                        expression, /g is optional and stands for --count=0,
                        /i == --ignore-case, /s == --pattern-dot-all, /m ==
                        --pattern-multiline).
  -c COUNT, --count COUNT
                        make COUNT replacements for every file (0 makes
                        unlimited changes, default).
  -l, --linear          apply pattern for every line separately. Without this
                        flag whole file is read into memory.
  -i, --ignore-case     ignore case of characters when matching
  --pattern-dot-all     with this flag, dot(.) character in pattern match also
                        new line character (see:
                        http://docs.python.org/2/library/re.html#re.DOTALL).
  --pattern-verbose     with this flag pattern can be passed as verbose(see:
                        http://docs.python.org/2/library/re.html#re.VERBOSE).
  --pattern-multiline   with this flag pattern can be passed as multiline(see:
                        http://docs.python.org/2/library/re.html#re.MULTILINE)
                        .
  --utf8, -u            Use UTF-8 in --encoding-input, --encoding-file and
                        --encoding-filesystem
  --encoding-input ENCODING_INPUT
                        set encoding for parameters like --pattern etc
                        (default for your system: ascii)
  --encoding-file ENCODING_FILE
                        set encoding for content of processed files (default
                        for your system: ascii)
  --encoding-filesystem ENCODING_FILESYSTEM
                        set encoding for paths and filenames (default for your
                        system: utf-8)
  -b, --no-backup       disable creating backup of modified files.
  -e EXT, --backup-extension EXT
                        extension for backup files(ignore if no backup is
                        created), without leading dot. Defaults to: "bak".
  --stdin               read data from STDIN(implies --stdout)
  --stdout              output data to STDOUT instead of change files in-
                        place(implies --no-backup)
  --verbose             show files and how many replacements was done
  --debug               show more informations
  -v, --version         show version and exit

Miscellaneous notes:
* regular expressions engine used here is PCRE, dialect from Python
* is required to pass either --pattern and -replace, or --pattern-and-
  replace argument
* if pattern passed to --pattern-and-replace has /g modifier, it
  overwrites --count value
* if neither /g modifier nor --count argument is passed, assume that
  --count is equal 1
* if only --count is given, this value is used
* if --eval-replace is given, --replace must be valid Python code, where
  can be used m variable.m holds MatchObject instance (see:
  http://http://docs.python.org/2/library/re.html#match-objects, for
  example:
    --eval-replace --replace 'm.group(1).lower()'
* regular expressions with non linear search read whole file to yours
  computer memory - if file size is bigger then you have memory in your
  computer, it fails
* parsing expression passed to --pattern-and-replace argument is very
  simple - if you use / as delimiter, then in your expression can't be
  used this character anymore. If you need to use same character as
  delimiter and in expression, then better use --pattern and --replace
  argument

Security notes:
* be carefull with --eval-replace argument. When it's given, value
  passed to --replace is eval-ed, so any not safe code will be executed!

Author:
Marcin Sztolcman <marcin@urzenia.net> // http://urzenia.net

HomePage:
http://mysz.github.io/subst/

Some examples?

Simple replace word ‘Hello’ with ‘Hi’ in data read from STDIN:

echo 'Hello World!' | subst -s 's/Hello/Hi/' -

Replace every IP address in form: 192.168.1.X (where X is few digits - single octet) with 192.168.0.X in /etc/hosts:

subst -p '(192\.168)\.1\.(10)' -r '\1.0.\2' /etc/hosts

Installation

subst should work on any platform where Python is available, it means Linux, Windows, MacOS X etc. There is no dependencies, plain Python power :)

To install, go to GitHub releases, download newest release, unpack and put somewhere in PATH (ie. ~/bin or /usr/local/bin).

If You want to install newest unstable version, then just copy file to your PATH, for example:

curl https://raw.github.com/mysz/subst/master/subst.py > /usr/local/bin/subst

or:

wget https://raw.github.com/mysz/subst/master/subst.py -O /usr/local/bin/subst

Voila!

Authors

Marcin Sztolcman marcin@urzenia.net

Contact

If you like or dislike this software, please do not hesitate to tell me about this me via email (marcin@urzenia.net).

If you find bug or have an idea to enhance this tool, please use GitHub’s issues.

License

The MIT License (MIT)

Copyright (c) 2013 Marcin Sztolcman

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

ChangeLog

v0.4.0

  • PEP8 improvements (coding style)

  • Makefile added

  • improved pylintrc

v0.3.1

  • prepared and uploaded to PYPI

  • typos and editorials

v0.3

  • better handling of non-ascii encoding in files, patterns etc

  • higher priority for –pattern-* switches then modifiers in –pattern-and-replace

  • unified switches syntax (was –pattern_and_replace, but other switches used dashes)

  • pep8

  • typos and editorials

v0.2

  • second public version

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

subst-0.4.0.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

subst-0.4.0-py2.py3-none-any.whl (14.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file subst-0.4.0.tar.gz.

File metadata

  • Download URL: subst-0.4.0.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for subst-0.4.0.tar.gz
Algorithm Hash digest
SHA256 489165efa1b5ab140fc26de0f577885be3a4f7507b0a3484496fb8bda039f229
MD5 c08e82d970844662c30b48edb2614d62
BLAKE2b-256 e6ea72047d83ae4e15bf181b0d274bce1a00a9c8e7b78ba7a9b413a34e9d3627

See more details on using hashes here.

File details

Details for the file subst-0.4.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for subst-0.4.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a6f8564da04835a6e7cd133122c5453f7705d427ee6ed47cf09a733114aa5f56
MD5 78126c90fc66bb863a050fe1541dfbbd
BLAKE2b-256 7bf7c56d07bca5ea51d206c03789f9e1827db56a4f41213d153158c34e2b1eac

See more details on using hashes here.

Supported by

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