Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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 tryied something like wc -l | grep vi file1

How it\s the proper way to do it ?

share|improve this question
    
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 at 9:20

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
1  
only grepping for vi will match all serVIces... –  Sylvain Pineau Apr 9 at 8:48

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
share|improve this answer
    
Thank you, but do you know how can I do it with the file? –  user253956 Apr 9 at 8:44
    
I've updated my answer –  Sylvain Pineau Apr 9 at 8:56
    
@SylvainPineau i think op want to display the lines where vi is presented at any position. –  Avinash Raj Apr 9 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 at 9:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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