referencing in cmd string

Terry Reedy tjreedy at udel.edu
Sun Sep 14 11:26:11 EDT 2003


"Kali K E" <kalike2003 at netscape.net> wrote in message
news:ab5a234a.0309140638.e81fbab at posting.google.com...
> Hi,
>
> I wanted to grep a file for a pattern. The pattern is a dynamic one
> stored in the string "rec". I tried to do it as follows:
>
> cmd = "grep -c rec filelist2 > grepdata"
> os.system(cmd)

I believe you want the value of the string named 'rec', and not 'rec'
itself, inserted in your command string.  If that value does not
itself need to be quoted within the command string, then this should
work.

"grep -c %s filelist2 > grepdata" % rec

If your pattern has chars that make it necessary to be (double?)
quoted, then maybe

'grep -c "%s" filelist2 > grepdata' % rec

will work.  (The command quoting rules depend on your OS and shell.)

Terry J. Reedy


is what you






More information about the Python-list mailing list