Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7639575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:27:14+00:00 2026-05-31T08:27:14+00:00

I have a command that looks like this: ps v -p 2585 | awk

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:27:16+00:00Added an answer on May 31, 2026 at 8:27 am

    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."""
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say that I have simple command line application, that looks something like this #
I have a command that runs fine if I ssh to a machine and
I have a complex command that I'd like to make a shell/bash script of.
I have a table that looks like this: ID / Description / textValue /
I have a folder organization that looks like this: link.sh dist/MyApp-3.0.0/script.sh dist/MyApp-3.0.0/lib/*.jar The link.sh
I have a list of file that looks like this 4 -rw-r--r-- 1 neversaint
I have a sample xml file that looks like this: <Books> <Category Genre=Fiction BookName=book_name
I have a messy html that looks like this: <div id=:0.page.0 class=page-element style=width: 1620px;>
I have an R data frame that looks like this: z = as.data.frame(list(Col1=c(a,c,e,g),Col2=c(b,d,f,h),Col3=c(1,2,5,3,5,7,9,8,1))) >
I have a directory that looks like this: pages/ folder1/ folder1.filename1.txt folder1.filename2.txt folder2/ folder2.filename4.txt

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.