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
  • 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 Apr 9, 2014 at 9:20

2 Answers 2

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
  • 1
    only grepping for vi will match all serVIces... Apr 9, 2014 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
4
  • Thank you, but do you know how can I do it with the file?
    – user253956
    Apr 9, 2014 at 8:44
  • I've updated my answer Apr 9, 2014 at 8:56
  • @SylvainPineau i think op want to display the lines where vi is presented at any position. Apr 9, 2014 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. Apr 9, 2014 at 9:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .