user1178682
user1178682

Reputation:

Invalid char in expression awk, python

I have a command that looks like this:

ps v -p 2585 | awk '{if ($9 != "%MEM") {print $9}}'

Now this runs fine in bash, it just takes the memory portion of whatever pid you give it. However I am now trying to implement it in python but I am having some issues. Here is what I have in python

cmd1 = ['ps', 'v', '-p', pid]
cmd2 = ['awk', '\'{if ($9 != "%MEM") {print $9}}\'']

Now to run them...

runcmd1 = subprocess.Popen(cmd1, stdout=subprocess.PIPE)
runcmd2 = subprocess.Popen(cmd2, stdin=runcmd1.stdout, stdout=subprocess.PIPE)

I get this error:

awk: '{if (\$9 != "%MEM") {print \$9}}'
awk: ^ invalid char ''' in expression

I used this to print out what the command looked like together... sys.stdout.write(' '.join(cmd1)+' '+'|'+' '+' '.join(cmd2)+'\n')

And it gave:

ps v -p 1073 | awk '{if ($9 != "%MEM") {print $9}}'

I see no difference between this and the actual bash command that works. Can anyone help?

Upvotes: 3

Views: 3669

Answers (2)

Useless
Useless

Reputation: 67782

You don't need to protect the awk commands from the shell when you're running it via popen (the arguments are already split into a list, so your whitespace is left alone).

cmd2 = ['awk', '{if ($9 != "%MEM") {print $9}}']

will work fine.


Note for future reference

Python has some nice ways of writing strings that avoid escaping like you tried to do here, for situations where you do need it:

'''In this string, I don't need to escape a single ' character,
   or even a new-line, because the string only ends
   when it gets three ' characters in a row like this:'''

"""The same is true of double-quotes like this.
Of course, whitespace and both the ' and " quote characters
are safe in here."""

Upvotes: 6

William Pursell
William Pursell

Reputation: 212464

When you run the command in bash, bash removes the single quotes and gives awk the first argument:

{if ($9 != "%MEM") {print $9}}

You are giving it single quotes, which you ought not do. You should just write:

cmd2 = ['awk', '{if ($9 != "%MEM") {print $9}}']

Upvotes: 2

Related Questions