[Tutor] Tutor Digest, Vol 152, Issue 3
anish singh
anish198519851985 at gmail.com
Sat Oct 1 22:50:18 EDT 2016
> On 01/10/16 09:16, anish singh wrote:
> > I am trying to implement grep to just increase my knowledge
> > about regular expression.
> >
> > Below is the program usage:
> > python test.py -i Documents/linux/linux/ -s '\w+_readalarm*'
> >
> > However, due to my lack of knowledge about string handling
> > in python, I am getting wrong results.
>
> Possibly, but we can't tell because
> a) You don't show us the code that parses your input
>
import os, sys, getopt
import re
import glob
def get_full_path(path, pattern):
for (dirpath, dirnames, filenames) in os.walk(path):
match = re.search(pattern, dirpath)
for filename in filenames:
if filename.endswith(('.c', '.h')):
yield os.path.join(dirpath, filename)
def read_file(file, pattern):
with open(file, 'r') as infile:
for line in infile:
match = re.compile(str(pattern)).match(line)
if match:
print(file + " " + match.group())
def main(argv):
path, f_pattern, s_pattern = '', '', ''
try:
opts, args =
getopt.getopt(argv,"hi:p:f:s:",["ifile=","file_pattern=","string_pattern="])
except getopt.GetoptError:
print 'test.py -i <path> -p <pattern>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'test.py -i <path>'
sys.exit()
elif opt in ("-i", "--ifile"):
path = arg
elif opt in ("-f", "--file_pattern"):
f_pattern = arg
elif opt in ("-s", "--string_pattern"):
s_pattern = arg.encode().decode('unicode_escape')
print(s_pattern)
files = get_full_path(path, f_pattern)
for file in files:
read_file(file, s_pattern)
if __name__ == "__main__":
main(sys.argv[1:])
> b) You don't show us your output/error message
>
output is only file names. I don't see any other output.
I am running it like this:
python test.py -i ~/Documents/linux-next/ -s '\w*_read_register\w*'
>
> How are you parsing the input? Are you using
> the argparse module? Or one of the older ones?
> Or are you trying to just test the values in sys.argv?
>
You can see the code now.
>
> How do you determine the input filename and the pattern?
>
Yes. Those are correct that is why i am getting all the file names.
You can run this code on any directory and see it just provides
the output as file names.
> Have you proved that those values are correct before you
> call your read_file() function?
>
Yes.
>
>
> > def read_file(file, pattern):
> > with open(file, 'r') as outfile:
>
> Since you are reading it it probably should be
> called infile?
>
Done.
>
> > for line in outfile:
> > match = re.compile(str(pattern)).match(line)
> > if match:
> > print(file + " " + match.group())
>
>
> > Can someone let me know how can I pass regular expression
> > from command line?
>
> The real issue here is probably how you parse the
> input line but we can't tell. The recommended module
> for doing that is argparse. Try reading the module
> documentation which includes many examples.
>
> If you are still having problems show us your full
> program plus any error messages
>
> > Whole code: http://ideone.com/KxLJP2
>
> Unless its very long (>100lines?) just post it
> in your email.
>
>
>
More information about the Tutor
mailing list