Counting lines for each application

Hi All,

I have a output that suppose to be like this (see below please)

App : Line counts
=== ==================
AAA: 100
BBB: 201
CCC: 137
DDD: 32
EEE: 55

for i in `ps -ef | grep App`; do print $i; done

This only shows

App :

AAA
BBB
CCC
DDD

How do I do line count for each "APP"?

I trie this command like, but it doesn't work. Any help is greatly appreciated.

for i in `ps -ef | grep App`; do print $i | wc -l; done

Show us your real output from

for i in `ps -ef | grep App`; do print $i; done

and real desired output after counting...

Output:

AAA:
BBB:
CCC:
DDD:
EEE:

Desired Output (including wc -l). AAA has 100 lines, BBB has 201 lines, and so on..

AAA: 100
BBB: 201
CCC: 137
DDD: 32
EEE: 55

Thank you.

try

for i in `ps -ef | grep App` ; do echo "$i: `cat $i|wc -l`" ; done
ps -ef |nawk '{a[$9]++}END{for (i in a) print i " : " a|"sort -k3nr"}'

Because the output from "ps -ef" varies widely in different variants of Unix and Linux it would really help to see a sample command and matching output from your particular system.
Please highlight in the sample output what constitutes "one App" for the purposes of counting.
We need to know where "App" can be found in "ps -ef" .

Sorry for not being clear. And thanks for your response..

Here is the output from the system

oracle 1822968 1 0 Sep 01 - 0:00 oracleAAA (LOCAL=NO)
oracle 1826878 1 0 Sep 01 - 0:00 oracleAAA (LOCAL=NO)
oracle 1831026 1 0 Sep 03 - 0:00 oracleAAA (LOCAL=NO)
oracle 1835072 1 0 Sep 01 - 0:00 oracleAAA (LOCAL=NO)
oracle 1839210 1 0 Sep 03 - 0:00 oracleAAA (LOCAL=NO)
oracle 1847480 1 0 Sep 01 - 0:00 oracleAAA (LOCAL=NO)
oracle 1851540 1 0 Aug 31 - 0:00 oracleAAA (LOCAL=NO)
oracle 1855674 1 0 Aug 31 - 0:00 oracleAAA (LOCAL=NO)
oracle 1867886 1 0 Sep 01 - 0:00 oracleAAA (LOCAL=NO)
oracle 1876030 1 0 Sep 01 - 0:00 oracleAAA (LOCAL=NO)
oracle 1884172 1 0 Sep 01 - 0:00 ora_mmon_AAA (LOCAL=NO)
oracle 1888358 1 0 Sep 01 - 0:00 oracleAAA (LOCAL=NO)
oracle 1892466 1 0 Sep 01 - 0:00 oracleAAA (LOCAL=NO)
oracle 1896460 1 0 Sep 01 - 0:00 oracleAAA (LOCAL=NO)
oracle 1900702 1 0 Sep 01 - 0:00 oracleAAA (LOCAL=NO)
oracle 1896460 1 0 Sep 01 - 0:00 oracleAAA (LOCAL=NO)
oracle 1900702 1 0 Sep 01 - 0:00 ora_ckpt_AAA
oracle 1896460 1 0 Sep 01 - 0:00 ora_qmnc_AAA
oracle 1900702 1 0 Sep 01 - 0:00 ora_psp0_AAA

oracle 1822968 1 0 Sep 01 - 0:00 oracleBBB (LOCAL=NO)
oracle 1826878 1 0 Sep 01 - 0:00 oracleBBB (LOCAL=NO)
oracle 1831026 1 0 Sep 03 - 0:00 oracleBBB (LOCAL=NO)
oracle 1835072 1 0 Sep 01 - 0:00 oracleBBB (LOCAL=NO)
oracle 1839210 1 0 Sep 03 - 0:00 oracleBBB (LOCAL=NO)
oracle 1847480 1 0 Sep 01 - 0:00 oracleBBB (LOCAL=NO)
oracle 1851540 1 0 Aug 31 - 0:00 oracleBBB (LOCAL=NO)
oracle 1900702 1 0 Sep 01 - 0:00 ora_ckpt_BBB
oracle 1896460 1 0 Sep 01 - 0:00 ora_qmnc_BBB
oracle 1900702 1 0 Sep 01 - 0:00 ora_psp0_BBB

I'd like to count how many lines for AAA and how many lines for BBB

The desired output I'm looking for is:

AAA: 19
BBB: 10

Appreciate your help. Thanks!

Given what it has been said already:

ps -ef | awk '{ token[substr($9,length($9)-2,3)]++} END{for (i in token) print i " : " token | "sort -k3nr" }' | grep -E '[A-Z]{3}'

or

ps -ef | awk --re-interval '$9 ~ /[A-Z]{3}/ { token[substr($9,length($9)-2,3)]++} END {for (i in token) print i " : " token | "sort -k3n" }'

Both lines will discard any other process that it doesn't have a pattern of: AAA, BBB, CCC, ... in the last 3 characters of the 9th field

for osid in AAA BBB
do
   echo "$osid: $(ps -u oracle -f|grep -c $osid)"
done
1 Like

Looks fine.

awk 'NF>0 {a[$9]++}END{for (i in a) print i " : " a|"sort -k3nr"}' infile

oracleAAA : 15
oracleBBB : 7
ora_ckpt_AAA : 1
ora_ckpt_BBB : 1
ora_mmon_AAA : 1
ora_psp0_AAA : 1
ora_psp0_BBB : 1
ora_qmnc_AAA : 1
ora_qmnc_BBB : 1

or do you mean you need count ora_psp0_AAA, ora_ckpt_AAA , and oracleAAA as AAA?

Hi Beginer0705,

If the output you have specified is content of the file called log2 then

$ cat log2 | grep -c AAA
19
$ cat log2 | grep -c BBB
10

I need to count ora_psp0_AAA, ora_ckpt_AAA , and oracleAAA as AAA.

---------- Post updated at 12:57 PM ---------- Previous update was at 12:45 PM ----------

This code works for me. Thanks everyone for your help.

for osid in AAA BBB
do
echo "$osid: $(ps -u oracle -f|grep -c $osid)"
done

ps -u oracle -f| awk '/AAA/ {AAA++} /BBB/{BBB++} END {print "AAA:", AAA; print "BBB:" , BBB}'
AAA: 19
BBB: 10