[Tutor] Searching 2 Strings in A File

Dave Angel d at davea.name
Wed Jun 6 21:46:40 CEST 2012

On 06/06/2012 03:33 PM, dohoang4093 at comcast.net wrote:
> i am writing a python script that will be invoked as follows: 
> myScript.py <an IP> 
> and the script is as follwos: 
> x = "Device " + sys.argv[1] + " restored the database" 
> y = "Created connection to " + sys.argv[1] 
> cmd_line = Popen(["egrep", "(x,y)", aLogFile], stdout=PIPE, stdin=PIPE, stderr=STDOUT) 
> the above code does not work because it will look for x and y in aLogFile instead of 
> looking for <"Device " + sys.argv[1] + " restored the database"> and <"Created connection to " + sys.argv[1]> 
> Any hint is appreciated. 
I'm not commenting on egrep and its parsing rules, but when you are
passing a list to Popen(), each item is a string.  Your second item is
"(x,y)"  when you want something like <"Device...
You have the word  "and" in there but I have no idea what you mean by
it.  Are there two arguments to egrep there, or one?  I'll assume one
for now.

So build another variable called arg1, and make it right.  The cmd_line
will then just be

  cmd_line = Popen(["egrep", arg1, aLogFile], stdout=PIPE, stdin=PIPE, stderr=STDOUT) 

arg1 *might* just be something like
    arg1 = x+y

but I seriously doubt it.  You'll probably be adding quotes, and maybe
even backslashes and other stuff to the mix.

If you showed us exactly what you're expecting the commandline of egrep
to look like, we might be able to come up with an answer.  But you
definitely don't want to try to do it all in one line.

Pick something simple and get that working, then make it more complex
till it meets your requirements.



More information about the Tutor mailing list