Solaris 9 - 単語数(wc)コマンドは "grep -v and ps"を使用して誤った出力を提供します。

Solaris 9 - 単語数(wc)コマンドは

私が使うとき'ps', 'egrep'そして「トイレ」プロセス数を計算するコマンドを実行すると、誤った値が表示されます。コマンドを実行しています。「ソラリス9」マシン。

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 $

出力には4行(プロセス3個+タイトル)のみが表示されます。ただし、以下のコマンドを使用すると、次のように出力が返されます。5

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

一方、出力をファイルにエコーし、行数を数えると完璧に動作します。

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

「egrep」と「egrep -v」に関する追加の説明:

「-v」オプションなしで「egrep」が機能するのはなぜですか?しかし、「-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$

ベストアンサー1

wc -l改行文字の数を計算します。意味のあるデータを含む行よりも出力に改行がある可能性があります。

例えば。 1行だけを含み、改行文字は含まないファイルを作成します。

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

$ wc -l < test.txt
0

おすすめ記事