Search code examples
pythonbashpython-2.7greppopen

Slashes in grep search string called from Python


I have the following input file "testFile.txt":

$ cat testFile.txt
111     // mNx
222     // mNy not nMx
333     // mNz also not nMx

I want to get the value of mNx, but some of the other lines contain comments about mNx. Using grep on the Unix command line to find the correct line:

$ grep mNx testFile.txt
111     // mNx
222     // mNy not mNx
333     // mNz also not mNx

However,

$ grep "// mNx" testFile.txt
111     // mNx

OK, so far so good, but I want to call grep using Python. Following on from this post I have

from subprocess import Popen, PIPE
def grep1(inFile, string):
    COMMAND = 'grep %s %s' % (string, inFile)
    process = Popen(COMMAND, shell=True, stderr=PIPE, stdout=PIPE)
    output, errors = process.communicate()
    return output

mNx = grep1('testFile.txt', 'mNx')
print mNx

which gives

111     // mNx
222     // mNy not mNx
333     // mNz also not mNx

Now if I instead use

mNx = grep1('testFile.txt', '// mNx')

it returns the following:

testFile.txt:111     // mNx
testFile.txt:222     // mNy not mNx
testFile.txt:333     // mNz also not mNx

I have tried "\/\/ mNx", r"// mNx", r"\/\/ mNx" etc., but cannot reproduce the native grep behaviour. Is something being escaped here in my Python string? What is going on?


Solution

  • Change the function call to mNx = grep1('testFile.txt', '"// mNx"').

    The thing is, we need the COMMAND to be this Python String Literal 'grep "// mNx" testFile.txt'

    You need to escape only if it is \. / can be represented as is in Python.