Solaris 9 - Word Count (wc) command gives wrong output

1

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. Please assist in understanding the issue.

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

Jass

Posted 2017-02-04T08:02:00.270

Reputation: 13

1Can you pipe the first example to hexdump -Cv and give the results? – iBug – 2017-02-04T11:44:01.263

What is the output of alias egrep ? – janos – 2017-02-04T12:54:33.863

@iBug Unfortunately hexdump is not availble in my server.

bash: hexdump: command not found – Jass – 2017-02-05T01:40:29.983

@janos

MyServer $ alias egrep bash: alias:egrep' not found` – Jass – 2017-02-05T01:42:15.440

1If available, install the package bsdmainutils from your package provider. The hexdump should be available. Or you can use busybox instead. – iBug – 2017-02-05T04:27:19.797

The 'wc' command was showing up on another line in the output, that wasn't showing up in the redirect. – Nevin Williams – 2017-02-08T03:58:16.907

Answers

0

I think it's counting the wc process, which won't show up when the output for ps is being piped to a file.

Try this:

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

Nevin Williams

Posted 2017-02-04T08:02:00.270

Reputation: 3 725