用replace实现grep

Greptile的Python项目详细描述


Build Status
Codacy Badge
codecov

灰色

python中的快速grep实现,具有replace特性

usage: greptile.py [-h] [-v] [-x EXTENSIONS [EXTENSIONS ...]] [-r] [-l] [-i]
                   [-g REPLACE_EXPR] [-f REPLACE_EXPR]
                   expression [file]

file search and replace with regular expressions

positional arguments:
  expression            regular expression
  file                  file path (or directory if -r is used)

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         show program's version number and exit
  -x EXTENSIONS [EXTENSIONS ...], --extensions EXTENSIONS [EXTENSIONS ...]
                        restrict search to file extensions (ex: .py .txt .java
                        .xml)
  -r, --recursive       recursively search in path
  -l, --list            list files matching pattern
  -i, --inplace         update the file inplace (with -g or -f)
  -g REPLACE_EXPR, --replace-global REPLACE_EXPR
                        global replacement expression
  -f REPLACE_EXPR, --replace-first REPLACE_EXPR
                        first occurrence replacement expression

为什么这么快?

因为它只使用发电机,爬行动物从不分配大的 列表,它总是一次处理一个文件中的一行。你可以做一个 递归搜索大文件和大目录,如/,而不搜索 内存开销。

示例:

搜索"import"in./greptile.py:

$ ./greptile.py "import" ./greptile.py
import re
import os
import sys
    import argparse

用python替换和简单分组re.sub语法:

$ greptile.py "\[(.*)\]\((.*)\)" README.md -g "<a href=\"\2\">\1</a>"| diff -u README.md -
--- README.md   2016-04-19 22:37:20.000000000 +0200
+++ -   2016-04-25 14:54:24.000000000 +0200
@@ -1,4 +1,4 @@
-![Agera](https://github.com/google/agera/blob/master/doc/images/agera.png)
+!<a href="https://github.com/google/agera/blob/master/doc/images/agera.png">Agera</a>
 Reactive Programming for Android
 ================================

原料药:

importgreptile# Replace "import" by "export" from dir `./`, in all files and these extensions: .py, .xml, .javagreptile.replace('import','export','./','.py','.xml','.java')# Return filenames of all files containing the text "import" from dir `./` and .py, .xml, .javamy_list=greptile.grep_rl('import','./','.py','.xml','.java')# same as calling replace()greptile.sed_i(my_list,'import','export')

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

推荐PyPI第三方库


热门话题
Java:字符串。RTL设备语言用标志“+”格式化,数字后加符号   java GAE作为桌面应用程序(Swing)的服务提供商   java将InputStream转换为FileInputStream不适用于Apache POI   java外部Voronoi库“网格”:什么是草图和处理?   重载重写的泛型方法java   java显示组织上设置的错误。springframework。验证。jsp中的错误对象   java一些Spring模型属性没有显示在我的JSP中   java无法编译Guava 23的SimpleTimeLimiter示例   java如何更改JTree中的“根”目录名?   java如何在安卓中对相对布局产生连锁反应?   java错误:org。冬眠例外SQLGrammarException:无法提取结果集,dateAccessed是未知列   如何使用java监听JSON文件更新   由抽象封闭类创建的匿名内部类能否通过反射确定实现类?