Grep and Python

I need a way to search for a file using grep through a regular expression from a Unix command line. For example, when I type in the command line:

python pythonfile.py 'RE' 'file-to-be-searched' 

I need the regular expression 'RE' to search the file and print the corresponding lines.

Here is the code I have:

 import re import sys search_term = sys.argv[1] f = sys.argv[2] for line in open(f, 'r'): if re.search(search_term, line): print line, if line == None: print 'no matches found' 

But when I enter a word that is not there, no matches found does not print

+62
python regex grep
Dec 17 '09 at 13:45
source share
6 answers

The natural question is why not just using grep ?! But if you canโ€™t ...

 import re import sys file = open(sys.argv[2], "r") for line in file: if re.search(sys.argv[1], line): print line, 

Notes:

  • search instead of match to find anywhere in the string
  • the comma ( , ) after print deletes the carriage return (the line will have one)
  • argv includes the python file name, so the variables must start at 1

This does not handle multiple arguments (e.g. grep does) or extends wildcards (e.g. Unix shell). If you need this functionality, you can get it using the following:

 import re import sys import glob for arg in sys.argv[2:]: for file in glob.iglob(arg): for line in open(file, 'r'): if re.search(sys.argv[1], line): print line, 
+72
Dec 17 '09 at 13:51
source share

Brief and effective memory:

 #!/usr/bin/env python # file: grep.py import re, sys map(sys.stdout.write,(l for l in sys.stdin if re.search(sys.argv[1],l))) 

This works like egrep (without too much error handling), for example:

 cat input-file | grep.py "RE" 

And here is one line:

 cat input-file | python -c "import re,sys;map(sys.stdout.write,(l for l in sys.stdin if re.search(sys.argv[1],l)))" "RE" 
+12
Aug 07 '14 at 11:46
source share

Adapted from grep to python .

Accepts a list of file names through [2:] , does not handle exception handling:

 #!/usr/bin/env python import re, sys, os for f in filter(os.path.isfile, sys.argv[2:]): for line in open(f).readlines(): if re.match(sys.argv[1], line): print line 

sys.argv[1] resp sys.argv[2:] works if you run it as a standalone executable, which means

chmod +x

the first

+5
Dec 17 '09 at 13:51
source share
  • use sys.argv to get command line options
  • use open() , read() to manage the file
  • use Python re module to match strings
+4
Dec 17 '09 at 13:47
source share

You might be interested in pyp . Referring to my other answer :

"The Pyed Piper", or pyp, is a linux command line text manipulation tool similar to awk or sed, but which uses the standard python string and list, as well as custom functions designed for quick creation, resulting in an intensive production environment.

+2
Jun 11 '13 at 9:32
source share

The real problem is that the variable string always matters. Checking for "no matches found" is to see if there is a match, so the code "if line == None:" should be replaced with "else:"

0
Jul 27 '18 at 17:11
source share



All Articles