mirror of
https://github.com/python/cpython.git
synced 2025-09-27 10:50:04 +00:00
Added pgrep() function, which is like grep/egrep/emgrep but uses Perl
syntax, by virtue of the new re module.
This commit is contained in:
parent
b49144244c
commit
8566e474b4
2 changed files with 36 additions and 0 deletions
18
Lib/grep.py
18
Lib/grep.py
|
@ -38,6 +38,24 @@ def ggrep(syntax, pat, files):
|
||||||
showline(filename, lineno, line, prog)
|
showline(filename, lineno, line, prog)
|
||||||
fp.close()
|
fp.close()
|
||||||
|
|
||||||
|
def pgrep(pat, *files):
|
||||||
|
if len(files) == 1 and type(files[0]) == type([]):
|
||||||
|
files = files[0]
|
||||||
|
global opt_show_filename
|
||||||
|
opt_show_filename = (len(files) != 1)
|
||||||
|
import re
|
||||||
|
prog = re.compile(pat)
|
||||||
|
for filename in files:
|
||||||
|
fp = open(filename, 'r')
|
||||||
|
lineno = 0
|
||||||
|
while 1:
|
||||||
|
line = fp.readline()
|
||||||
|
if not line: break
|
||||||
|
lineno = lineno + 1
|
||||||
|
if prog.search(line):
|
||||||
|
showline(filename, lineno, line, prog)
|
||||||
|
fp.close()
|
||||||
|
|
||||||
def showline(filename, lineno, line, prog):
|
def showline(filename, lineno, line, prog):
|
||||||
if line[-1:] == '\n': line = line[:-1]
|
if line[-1:] == '\n': line = line[:-1]
|
||||||
if opt_show_lineno:
|
if opt_show_lineno:
|
||||||
|
|
|
@ -38,6 +38,24 @@ def ggrep(syntax, pat, files):
|
||||||
showline(filename, lineno, line, prog)
|
showline(filename, lineno, line, prog)
|
||||||
fp.close()
|
fp.close()
|
||||||
|
|
||||||
|
def pgrep(pat, *files):
|
||||||
|
if len(files) == 1 and type(files[0]) == type([]):
|
||||||
|
files = files[0]
|
||||||
|
global opt_show_filename
|
||||||
|
opt_show_filename = (len(files) != 1)
|
||||||
|
import re
|
||||||
|
prog = re.compile(pat)
|
||||||
|
for filename in files:
|
||||||
|
fp = open(filename, 'r')
|
||||||
|
lineno = 0
|
||||||
|
while 1:
|
||||||
|
line = fp.readline()
|
||||||
|
if not line: break
|
||||||
|
lineno = lineno + 1
|
||||||
|
if prog.search(line):
|
||||||
|
showline(filename, lineno, line, prog)
|
||||||
|
fp.close()
|
||||||
|
|
||||||
def showline(filename, lineno, line, prog):
|
def showline(filename, lineno, line, prog):
|
||||||
if line[-1:] == '\n': line = line[:-1]
|
if line[-1:] == '\n': line = line[:-1]
|
||||||
if opt_show_lineno:
|
if opt_show_lineno:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue