使用 python 递归 grep

发布于 2024-11-28 22:05:26 字数 424 浏览 1 评论 0原文

我是Python新手,正在努力学习。我正在尝试使用 python 实现一个简单的递归 grep 进行处理,这就是我到目前为止所得到的。

p = subprocess.Popen('find . -name [ch]', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  for line in p.stdout.readlines():
    q = subprocess.Popen('grep searchstring %s', line, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    print q.stdout.readlines()

有人可以告诉我如何解决这个问题以完成它应该做的事情吗?

I am new to python and trying to learn. I am trying to implement a simple recursive grep using python for processing and here is what I came to so far.

p = subprocess.Popen('find . -name [ch]', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  for line in p.stdout.readlines():
    q = subprocess.Popen('grep searchstring %s', line, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    print q.stdout.readlines()

Can some one pls tell me how to fix this to do what it is supposed to?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黑白记忆 2024-12-05 22:05:26

您应该使用 os.walk 函数来浏览文件。使用字符串方法或正则表达式来过滤结果。检查 http://docs.python.org/library/os.html 了解有关如何使用 os.walk。

import os
import re

def findfiles(path, regex):
    regObj = re.compile(regex)
    res = []
    for root, dirs, fnames in os.walk(path):
        for fname in fnames:
            if regObj.match(fname):
                res.append(os.path.join(root, fname))
    return res

print findfiles('.', r'my?(reg|ex)')

现在对于 grep 部分,您可以使用 open 函数循环遍历文件。

def grep(filepath, regex):
    regObj = re.compile(regex)
    res = []
    with open(filepath) as f:
        for line in f:
            if regObj.match(line):
                res.append(line)
    return res

如果您想获取行号,您可能需要查看 enumerate 函数。

编辑添加 grep 函数

You should use the os.walk function for going through your files. Use string methods or regex for filtering out the results. Check http://docs.python.org/library/os.html for informations about how to use os.walk.

import os
import re

def findfiles(path, regex):
    regObj = re.compile(regex)
    res = []
    for root, dirs, fnames in os.walk(path):
        for fname in fnames:
            if regObj.match(fname):
                res.append(os.path.join(root, fname))
    return res

print findfiles('.', r'my?(reg|ex)')

Now for the grep part, you can loop over the file with the open function

def grep(filepath, regex):
    regObj = re.compile(regex)
    res = []
    with open(filepath) as f:
        for line in f:
            if regObj.match(line):
                res.append(line)
    return res

If you want to get the line numbers, you may want to look into the enumerate function.

edited to add the grep function

在你怀里撒娇 2024-12-05 22:05:26

您可以使用 python-textops3 :

示例,从当前目录中的所有 .py 文件中 grep 所有“导入”:

from textops import *

print('\n'.join(('.' | find('*.py') | cat() | grep('import')))) 

它是纯 python,无需分叉进程。

You can use python-textops3 :

Example, to grep all 'import' in all .py files from current directory :

from textops import *

print('\n'.join(('.' | find('*.py') | cat() | grep('import')))) 

It is pure python, no need to fork a process.

小女人ら 2024-12-05 22:05:26
p = subprocess.Popen('find . -name [ch]', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  for line in p.stdout.readlines():
    q = subprocess.Popen('grep searchstring %s', line, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    print q.stdout.readlines()
  1. 第2行的缩进会异常,for需要与
  2. 'grep searchstring %s'上面的p对齐,line不行字符串替换,您需要将 , 替换为 %

通过这些更改和实际搜索值,它可以在我的 OS X 机器上运行。最终脚本:

import subprocess
p = subprocess.Popen('find . -name *.py', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
    print line
    q = subprocess.Popen('grep import %s' % line, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    print q.stdout.readlines()
p = subprocess.Popen('find . -name [ch]', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  for line in p.stdout.readlines():
    q = subprocess.Popen('grep searchstring %s', line, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    print q.stdout.readlines()
  1. The indentation on line 2 will Exception, the for needs to be aligned with the p above
  2. 'grep searchstring %s', line will not do the string replacement, you need to replace the , with %

With those changes and real search values, it works on my OS X box. Final script:

import subprocess
p = subprocess.Popen('find . -name *.py', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
    print line
    q = subprocess.Popen('grep import %s' % line, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    print q.stdout.readlines()
似最初 2024-12-05 22:05:26

也许一个例子可以帮助您,命令 find 。 -打印| grep "python" 相当于:

import subprocess

pc1 = subprocess.Popen('find . -print', stdout=subprocess.PIPE, shell=True)
pc2 = subprocess.Popen('grep "python"', stdin=pc1.stdout, shell=True,
                       stdout=subprocess.PIPE)

print pc2.communicate()[0]

Maybe an example can help you, the command find . -print | grep "python" is equivalent to this:

import subprocess

pc1 = subprocess.Popen('find . -print', stdout=subprocess.PIPE, shell=True)
pc2 = subprocess.Popen('grep "python"', stdin=pc1.stdout, shell=True,
                       stdout=subprocess.PIPE)

print pc2.communicate()[0]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文