2

First of all I save in a file all of the working processes.

ps -el > file1

My idea is to count the number of lines in file1 where vi is present.

I tried something like wc -l | grep vi file1

How is the proper way to do it ?

  • 1
    Could you please explain if you're looking for all VI processes (VI the text editor) or processes containing vi, to match not only vi or vim but services as well. Thanks – Sylvain Pineau Apr 9 '14 at 9:20
1

Your code nearly works, you just have to change

wc -l | grep vi file1

to

grep vi file1 | wc -l

The pipe operator uses the output of the program on the left as input for the program on the right.

  • 1
    only grepping for vi will match all serVIces... – Sylvain Pineau Apr 9 '14 at 8:48
0

Why not using pgrep:

pgrep "^vi" | wc -l

pgrep man page:

SYNOPSIS

   pgrep [options] pattern

DESCRIPTION

   pgrep looks through the currently running processes and lists the process 
   IDs which matches the selection criteria to stdout.

EDIT: (Using the file):

ps aux > file1
awk '{ print $11 }' file1 | egrep '^vi' | wc -l
  • Thank you, but do you know how can I do it with the file? – user253956 Apr 9 '14 at 8:44
  • I've updated my answer – Sylvain Pineau Apr 9 '14 at 8:56
  • @SylvainPineau i think op want to display the lines where vi is presented at any position. – Avinash Raj Apr 9 '14 at 9:10
  • Humm, in that case to match all "vi" patterns, remove the "^" before "^vi" so that you'll match not only vi, vim but also services like indicator-session-service, unity-webapps-service. – Sylvain Pineau Apr 9 '14 at 9:18

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.