` subst`是一个简单的实用程序,用于将给定文件列表中的一个字符串替换为另一个字符串。

subst的Python项目详细描述


subst是一个简单的实用程序,用于在给定的 文件列表。

当前稳定版本

0.4.0

但为什么呢?

  1. 例如有sed

    是的,是的。但是sed使用名为“basic regular”的regexps引擎 “表达式”,或“扩展正则表达式”。PCRE更重要 广泛使用的方言。

  2. 所以我可以使用perl!

    你当然可以。但并不是每个人都知道如何使用Perl。我知道,但是 subst使用起来非常简单。

好吧,那怎么用呢?

简单

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

或:

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

更多

一切都在帮助中:)只需执行:

subst --help

查看结果:

% 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/

一些例子?

在从stdin读取的数据中,简单地将单词“hello”替换为“hi”:

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

替换格式为192.168.1.x的每个IP地址(其中x是几个数字- 一个八位字节),192.168.0.x英寸/etc/hosts

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

安装

subst应该在任何平台上工作 Python可用,表示Linux、Windows, MacOS X等。没有依赖项,纯Python功能:)

要安装,请转到GitHub releases,下载最新的 释放、解包并放在PATH(即~/bin/usr/local/bin)。

如果要安装最新的不稳定版本,请将文件复制到 您的路径,例如:

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

或:

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

喂!

作者

马辛·斯托尔克曼marcin@urzenia.net

联系人

如果你喜欢或不喜欢这个软件,请不要犹豫告诉我 关于这个我通过电子邮件(marcin@urzenia.net)。

如果你发现了bug或者有改进这个工具的想法,请使用 github的issues

许可证

麻省理工学院许可证(MIT)

版权所有(c)2013 Marcin Sztolcman

特此免费向任何获得 本软件和相关文档文件的副本 “软件”),无限制地处理软件,包括 但不限于使用、复制、修改、合并、发布, 分发、再授权和/或出售软件副本,以及 允许向其提供软件的人员这样做,但须遵守 以下条件:

上述版权公告及本许可公告须包括在内 在软件的所有副本或大部分中。

软件按“原样”提供,不作任何形式的保证 或默示的,包括但不限于 适销性、适合特定目的和不侵权。 在任何情况下,作者或版权持有人均不对任何 索赔、损害赔偿或其他责任,无论是在合同诉讼中, 侵权行为或其他,由 软件或软件中的使用或其他交易。

更改日志

v0.4.0

  • PEP8改进(编码风格)
  • 添加了makefile
  • 改进的Pylintrc

v0.3.1

  • 准备并上传至PYPI
  • 排版和社论

v0.3

  • 更好地处理文件、模式等中的非ascii编码
  • 更高优先级-模式-*开关,然后在 –打上图案并更换
  • 统一的开关语法(was–pattern_and_replace,but other 开关使用破折号)
  • PEP8
  • 排版和社论

v0.2

  • 第二个公开版本

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
JavaSpring异步数据库交互   java中的正则表达式模式/数组问题   swing在Java中设置按钮的位置   java如何实现基于松散耦合的代码散播主方法   velocity模板渲染的java过滤器问题   java如何在liferay中使用EmailAddressLocalService   多线程如何确保长时间运行的Java线程永不消亡   java如何以编程方式在Android启动器之间切换   安卓中的java解析查询问题   Java中的变量浅拷贝整数   SimpleDataFormat中月份的java奇怪问题   java如何在输入流中间读取偏移量?   使用MyComparator类的java排序ArrayList   java HeapDumpOnOutOfMemoryError创建空的hprof文件   java“around”通知的内容可以从单独的函数执行吗?   java将组件添加到我的GridBagLayout会将其他组件移到最右边   带有“WHERE”选择参数的java Android SQLite查询不起作用   安卓 Java NPE错误(尝试在空对象引用上调用虚拟方法)   java Print 2并排排列的ArrayList