Hello, I currently have the following python code:

#usr/bin/python
import os

variable=raw_input('Search for this string: ')
os.system("grep $variable sample.txt")

Basically what I want to do is to grep a string found in the text file "sample.txt" When I run the code, it just hangs. How can I make it so that grep accepts the variable?

Recommended Answers

first: Why shell out to the system to get pattern matching when python offers the re package?
second: call it like this:

os.system("grep %s sample.txt"%variable)
Jump to Post

And you have to supply the path unless sample.txt is in your PYTHONPATH.

os.system("grep %s ./sample.txt" % variable)  ## current directory
Jump to Post

All 5 Replies

first: Why shell out to the system to get pattern matching when python offers the re package?
second: call it like this:

os.system("grep %s sample.txt"%variable)

And you have to supply the path unless sample.txt is in your PYTHONPATH.

os.system("grep %s ./sample.txt" % variable)  ## current directory

griswolf, thank you so much. However, how can I make it so it searches for an exact phrase (you vs. you're).

Also, I am unfamiliar with the re package. Looks like i'll have to run a google search.

Python documentation: http://docs.python.org and in particular http://docs.python.org/library/re.html

If you are comfortable using grep or egrep, then the re package will be reasonably easy to learn, since it is just python syntax wrapped around (almost) the same regular expressions you would use with egrep.

I would do the search myself as for me Python is more powerful in many cases as regular expression language, which is like code language.

How ever, as they say: "Somebody likes the daughter, somebody else the mother."

Here my 5 minute code with text from above link of re documentation:

filename="sample.txt"
variable=raw_input('Search for this string: ').lower()

res= [line for line in open(filename) if variable in line.lower()]

for i in res:
      print i,
      print '-'*20
""" Output: (text file from re module documentation)
Search for this string: re.match
>>> re.match("c", "abcdef")  # No match
--------------------
    result = re.match(pattern, string)
--------------------
    The compiled versions of the most recent patterns passed to re.match(), re.search() or re.compile() are cached, so programs that use only a few regular expressions at a time needn?t worry about compiling regular expressions.
--------------------
re.match(pattern, string[, flags])¶
--------------------
class re.MatchObject¶
--------------------
        >>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
--------------------
        >>> m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)", "Malcolm Reynolds")
--------------------
        >>> m = re.match(r"(..)+", "a1b2c3")  # Matches 3 times.
--------------------
        >>> m = re.match(r"(\d+)\.(\d+)", "24.1632")
--------------------
        >>> m = re.match(r"(\d+)\.?(\d+)?", "24")
--------------------
        >>> m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)", "Malcolm Reynolds")
--------------------
# Error because re.match() returns None, which doesn't have a group() method:
--------------------
    re.match(r".*(.).*\1", "718ak").group(1)
--------------------
>>> re.match('Begin (\w| )*? end', s).end()
--------------------
>>> re.match("o", "dog")  # No match as "o" is not the first letter of "dog".
--------------------
The following applies only to regular expression objects like those created with re.compile("pattern"), not the primitives re.match(pattern, string) or re.search(pattern, string).
--------------------
>>> re.match(r"\W(.)\1\W", " ff ")
--------------------
>>> re.match("\\W(.)\\1\\W", " ff ")
--------------------
>>> re.match(r"\\", r"\\")
--------------------
>>> re.match("\\\\", r"\\")
--------------------
>>> """
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.