跳转到主要内容

pyf:程序员查找

项目描述

发音是“pif”。

pyf [options] [search-pattern [filename-pattern [start-directory]]]

递归搜索与搜索模式匹配的内容的文件。

  • 可选地限制搜索到与文件名模式匹配的文件。

  • 可选地更改目录到起始目录后再开始搜索。

  • 模式是Python正则表达式。

编写这个程序是因为我厌倦了重复编写

find . -name '*.py' -exec egrep -l regex {} \;

上面的代码

pyf regex '\.py$'

或者

pyf regex 'py$'

或者直接

pyf regex py

如果您没有传递正则表达式作为文件名模式,pyf假定它是一个文件扩展名匹配,并在末尾添加一个$。

文件名模式和文件内的搜索模式都是 Python正则表达式

示例

查找包含正则表达式的文件

pyf regex

上面的示例将递归地找到文件并在文件中搜索“regex”。

将搜索限制为特定的文件扩展名

pyf regex py

上面的示例将递归地找到以“py”结尾的文件,并在文件中搜索“regex”。

查找包含某物但不包含另一物的文件

pyf post html | pyf -v -f - csrf_token

上面的示例将找到所有以“html”结尾且包含“post”但不包含“csrf_token”的文件。

在匹配的文件上运行命令

pyf -r "sed -i '' -e 's/yajogo\.core\.debug/yajogo.core.logging/g'" 'yajogo\.core\.debug' py

以上代码查找扩展名为‘py’的文件,其中包含字符串‘yajogo.core.debug’,并在这些文件上运行sed命令。

打印正则表达式匹配结果

pyf -s -m '\d+x\d+' html

以上代码将打印以‘html’结尾的文件中所有模式‘+x+’的匹配结果。-s选项抑制打印匹配的文件名。-m选项使得匹配的正则表达式被打印。因此,您可能会得到如下输出:

57x57
72x72
114x114
512x512
200x200
150x150
150x150
150x150
500x500
800x600
150x150
150x150

我们可以将此命令的输出管道传递给另一个程序。例如:

pyf -s -m html '(\d+x\d+)' | sort | uniq

这将给出一个匹配结果的排序和唯一列表

114x114
150x150
200x200
500x500
512x512
57x57
72x72
800x600

安装

pip install pyf-programmers-find

用法

$ pyf -h
usage: pyf [options] [search-pattern [filename-pattern [start-directory]]]

pyf: programmers find

Recursively search for files whose contents matches search-pattern.
Optionally restrict the search to files whose name matches filename-pattern.
Patterns are Python regular expressions.

It's pronounced "pif".

positional arguments:
  search-pattern        Match this pattern in files.
  filename-pattern      Only search files whose name matches this pattern.
  start-directory       Change to this directory before findind and searching
                        files.

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  --debug               Turn on debug logging.
  --debug-log FILE      Save debug logging to FILE.
  -c COUNT, --context COUNT
                        Show COUNT surrounding context lines of the matches.
                        Only makes sense when printing matched lines with the
                        -p option. Default 0.
  -d START_DIRECTORY, --chdir START_DIRECTORY
                        Change to directory START_DIRECTORY before starting
                        the search. Can also be given as the third positional
                        argument.
  -e SEARCH_PATTERN, --regexp SEARCH_PATTERN
                        Use SEARCH_PATTERN as the pattern to match in a file;
                        use when defining patterns beginning with -. Can also
                        be given as the first positional argument.
  -f FILE, --file FILE  File to search for a match. Instead of recursively
                        searching all files. Can be given multiple times. If
                        argument is - reads a list of files to match from
                        stdin.
  -i, --ignore-case     Ignore case. Default False.
  -l, --line-number     Print the matching line number. Default False.
  -m, --matches         Print the matching regex group. Default False.
  -n FILENAME_PATTERN, --filename FILENAME_PATTERN
                        Recursively find files whose name matches
                        FILENAME_PATTERN. Only search in those files. Can also
                        be given as the second positional argument. Default:
                        .+
  -p, --print-lines     Print the matching line. Default False.
  -r CMD, --run CMD     Run a program CMD for each matching file, passing the
                        path name of the matching file as an argument. Ignored
                        if the -p or -l options are given.
  -s, --no-filename     Do not print the file name when printing matched
                        lines. Only makes sense with the -p option. Default
                        False.
  -v, --invert-match    Invert the sense of the match. Print non-matching
                        files and lines. Default False.
  -A, --suppress-file-access-errors
                        Do not print file/directory access errors.
  -B, --no-binary-check
                        Ignore (heuristic) binary file check, do not skip
                        probably binary files.
  -N, --no-pager        Do not pipe output to a pager when stdout it detected
                        as a tty.
  --force-pager         Always try to pipe output to a pager, do not check if
                        stdout is a tty. Ignored when running with the -r
                        option.
  --skip-dirs-pattern SKIP_DIRS_PATTERN
                        Regex of directories to skip. Default
                        '(^\..+|CVS|RCS|__pycache__)'.
  --skip-files-pattern SKIP_FILES_PATTERN
                        Regex of files to skip. Default '(^\..+|\.pyc$)'.

项目详情


下载文件

下载适合您平台的应用文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

pyf-programmers-find-0.9.0.tar.gz (17.5 kB 查看哈希)

上传时间:

构建分布

pyf_programmers_find-0.9.0-py2.py3-none-any.whl (14.9 kB 查看哈希)

上传时间: Python 2 Python 3

由以下机构支持