3

I have to show the PID, PPID and STATUS of the procceses on the system, but only the ones which PID starts with numbers in the range 5-8. So i tried something like this...

ps xao pid,ppid,s | grep ' 5'

But then I don’t know how to do it for the range, I can make it work for one number, but not for all of them.

3
ps xao pid,ppid,s | grep '^ *[5-8]'

If the PID is five digits, you don't have a space at the beginning of the line, hence the ' *' part after grep. '^'^ only searches at the beginning (thereby not selecting PPID is starting with 5,6,7,80 and. '[5-8]' handles the range of numbers you wanted (could also do '[5678]')

2

Try this :

ps xao pid,ppid,s | grep '^ *[5-8]'
  • ^ means: start of line (or string)
  • * is a quantifier (for preceding character) : zero or N
  • [] is the meta-character to indicate a range, here 5 to 8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.