1

I have a text file in a subdirectory of the current directory with the name P_R.txt which contains a list of processes (output of the command ps -aux). The file looks like this:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  3.0  0.3 137476  7428 ?        Ss   16:13   0:05 /sbin/init
root         2  0.0  0.0      0     0 ?        S    16:13   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    16:13   0:00 [ksoftirqd/0]
root         4  0.0  0.0      0     0 ?        S    16:13   0:00 [kworker/0:0]

I need to display processes which have a PID smaller then some number R

1
  • 1
    PID can not be only 4 digital number as far as i know – advardo Dec 16 '16 at 16:11
2

Replace subdir with the name of your subdirectory and R with your number...

awk '{if ($2 < R) print $0}' subdir/P_R.txt
  • if ($2 < R) if the number in the second column is less than R...
  • print $0 print the whole line

If you just want the process names then just print that column:

awk '{if ($2 < R) print $11}' subdir/P_R.txt
3
  • @advardo it's a pleasure :) – Zanna Dec 16 '16 at 16:33
  • Does awk ignore the first header row (USER PID etc)? – user423626 Dec 17 '16 at 12:29
  • @BharadwajRaju yes it does ignore it – Zanna Dec 17 '16 at 12:36

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.