0

In some of my python-scripts, I got glob.glob(parent_folder_path + glob_str). These lines with the associated files shall be printed out in the console with a proper grep command, but so far I couldn't find out how.

The one which worked for finding all the glob.glob - lines is the following:

grep --exclude-dir='.history' --exclude-dir='__pycache__' --exclude-dir='.wine' -Rinw /home/andylu/Desktop/Python/Scripts/ -e 'glob.glob'

Still it's not the preciseness I would like to achieve. When using 'glob.glob(p*' as the regex-string, only a line with glob.glob() appears in the search result. I couldn't make sense out of this since (p* gave me results with ().

Next, I tried 'glob.glob(p.*' which indeed worked out and showed me all results with glob.glob(parent_folder_path + glob_str).

Even though I managed to find it out via trial and error, I'd like to understand the grep-syntax better. Did the last regex-string work because, according to this cheat-sheet, . stands for any character and * for any number of repeated times?

2
  • p*. means zero-or-more occurrences of p character followed by a single character; what pattern do you want to match? answer based on title of the question you only need (p\w+. – αғsнιη Dec 21 '20 at 21:00
  • Sorry, my bad. I actually had 'glob.glob(p.*' working, that's what I wanted to state here, but wrote initially 'glob.glob(p*.'. Thanks for the hint. Your suggestion of (p\w+ did not work though. – Andreas L. Dec 21 '20 at 21:14
1

You are very close in your guess. In a regular expression, the sequence p*. means "zero or more of the character p, followed by exactly one of any character":

  • * - Zero or more of the preceding token
  • . - Exactly one character

The more common "global wildcard" in regular expression is .* which means "One or more of any type of character".

Your guess worked because you did not have a $ token (indicating "end of domain") in your expression.

In this case you would probably want the expression /glob\.glob(p.*/:

  • glob\.glob(p - the literal string glob.glob(p, followed by
  • .* - at least one of any type of character

To expound further, if for example you wanted to match more broadly all calls to glob.glob(), you could do something like /glob\.glob\([a-z_+ ]+\)/:

  • glob\.glob\( - the literal string glob.glob(
  • [a-z_+ ]+ - one or more of any of: any letter a-z, an underscore, a +, or a space
  • \) - a literal )
5
  • Thanks for the explanation, now I've fully understood why my final approach worked. Nevertheless, I tried out your most elaborate suggestion without a single result returned. Is this due the fact that grep -e doesn't understand certain regex-syntax? The one I tried based in your final suggestion was: grep --exclude-dir='.history' --exclude-dir='__pycache__' --exclude-dir='.wine' -Rinw /home/andylu/Desktop/Python/Scripts/ -e '/glob\.glob\([a-z_+ ]+\)/' --> no result returned – Andreas L. Dec 21 '20 at 21:21
  • Apologies for being unclear- the leading and trailing slashes are a common identifier for "this is a regular expression" (comparable to how quotation marks are a common identifier for "this is an exact quotation") - the actual expression itself does not include them. – DopeGhoti Dec 21 '20 at 21:31
  • Okay, now I've removed the quotation marks '..' around your suggested regex, so grep -Rinw /home/andylu/Desktop/Python/Scripts/ -e /glob\.glob\([a-z_+ ]+\)/ results in an error grep: Unmatched [, [^, [:, [., or [=. I don't know how to employ your suggested regex correctly. – Andreas L. Dec 21 '20 at 21:39
  • 1
    When I simply run grep -E 'glob\.glob\([a-z_+ ]+\)' foo.py on an test file, it works perfectly. In your case, try replacing the -e with -Ee to use an Extended Regular Expression, which I believe we need because of the + token. – DopeGhoti Dec 21 '20 at 21:44
  • 1
    Finally, that was it: we need both e-flags Ee to make it work, put the regex into quotes and remove the leading and trailing slashes. The working result is: grep /home/andylu/Desktop/Python/Scripts/ -Ee 'glob\.glob\([a-z_+ ]+\)' – Andreas L. Dec 21 '20 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.