-1

This question already has an answer here:

When i use 'ps', 'egrep' and 'wc' command to count number of processes, it gives me wrong value. I am running the command in 'Solaris 9' machine.

MyServer $ ps -fu root | egrep -v 'bash|ksh|ssh|ef|mailx|nohup|defunct|ps|sh|FND'
     UID   PID  PPID  C    STIME TTY      TIME CMD
 root 16267 16171  0   Jan 28 ?        0:18 xyz
 root 16269 16171  0   Jan 28 ?        0:07 abc
 root 16268 16171  0   Jan 28 ?        0:07 ghi
MyServer $

Only 4 (3 processes + header) lines shown in output. But when i use below command it returns the output as 5

MyServer $ ps -fu root | egrep -v 'bash|ksh|ssh|ef|mailx|nohup|defunct|ps|sh|FND' | wc -l
       5

On the otherhand, when I echo the output to a file and count the number of lines, it works perfectly normal.

MyServer $ ps -fu root | egrep -v 'bash|ksh|ssh|ef|mailx|nohup|defunct|ps|sh|FND' >temp && cat temp | wc -l
       4

More description related to 'egrep' and 'egrep -v':

Why does 'egrep' working properly without '-v' option? But not with '-v'?

MyServer$ ps -fu root | egrep '20755|13800'
 root 20755 20751  0 12:14:29 pts/5    0:00 more -s /tmp/mpWGa4mO
 root  1223 26407  0 12:57:28 pts/15   0:00 egrep 20755|13800
MyServer$ ps -fu root | egrep '20755|13800' | wc -l
       2
MyServer$

MyServer$ ps -fu root | egrep -v 'usr|apps|sql|sh'
     UID   PID  PPID  C    STIME TTY      TIME CMD
 root  3448 26407  0 12:57:42 pts/15   0:00 ps -fu root
 root 20755 20751  0 12:14:29 pts/5    0:00 more -s /tmp/mpWGa4mO
MyServer$ ps -fu root | egrep -v 'usr|apps|sql|sh' | wc -l
       4
MyServer$

marked as duplicate by JdeBP, Kusalananda, Archemar, Mat, Jeff Schaller Feb 4 '17 at 11:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    ... is wc a process? – steeldriver Feb 4 '17 at 8:32
  • on a side note, there is a funny option of grep that is -c .... – Archemar Feb 4 '17 at 8:58
  • The problem seems to be with 'egrep -v' option. Question has been modified with details that supports the issue. – Jass Feb 5 '17 at 2:06
  • @jass, when you redirected the grep result to file before wc -l.The result will be different.You should put wc -l before you redirected to file.When you used wc -l after the result file.That meant wc -l counted on file. – supriady Feb 5 '17 at 3:22
-1

wc -l counts new line characters. There may be more new line characters than lines with meaningful data in your output.

For example. Create a file with a line but without a newline character:

$ cat > test.txt
hello # Type CTRL+D (2 times are necessary on my system)

$ wc -l < test.txt
0

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