Need specific output from script

Hallo Team,

I have *.csv files and this is the command that i run to find out how many occurances i have in this files. For example i run this command below:

-bash-3.2$ grep "ACCOUNT NOT FOUND" BW-CDR-2013* |grep -v INCE|cut -f32 -d"," | sort -u
BRY
CORONATION-CONF
DOE-CONF
HIP-ADVTECH-AP
HIP-ADVTECH-NET-MENLYN
HIP-GEMS-NELSPRUIT
HIP-KELLY-JHB
HIP-MGS-JHB
HIP-ROD-LAB
HIP-YKK-JHB
JSE-CONF
PEARSON-CPT
SANLAM-SKY-DBN-BENEFITS
SCRANTON-PA
SI_FUTURES_PANGEA
THECAMPUS-GSC
TRANSUNION-CONF
TTS-FAX

As you can see i am using field 32 to get my results but the problem is the output changes every 4 hours. Sometimes you can have 24 lines returned or 100 lines returned.

So from the output i get i type in this command for example:

-bash-3.2$ grep HIP-ADVTECH-NET-MENLYN BW*2013*|wc -l
248

So i would like to automate everything where by field 32 would be fed into command 2.

I hope i am making sense.

What is command 2? Or, why does it matter how lines command 2 gets?

Do you run these commands with glob turned off? - For one issue, the * metacharacters would break grep. So, as presented, I cannot understand how those commands would work at all.

Command 1

-bash-3.2$ grep "ACCOUNT NOT FOUND" BW-CDR-2013* |grep -v INCE|cut -f32 -d"," | sort -u
BRY
CORONATION-CONF
DOE-CONF
HIP-ADVTECH-AP
HIP-ADVTECH-NET-MENLYN
HIP-GEMS-NELSPRUIT
HIP-KELLY-JHB
HIP-MGS-JHB
HIP-ROD-LAB
HIP-YKK-JHB
JSE-CONF
PEARSON-CPT
SANLAM-SKY-DBN-BENEFITS
SCRANTON-PA
SI_FUTURES_PANGEA
THECAMPUS-GSC
TRANSUNION-CONF
TTS-FAX

Command 2

-bash-3.2$ grep HIP-ADVTECH-NET-MENLYN BW*2013*|wc -l
248

---------- Post updated at 02:46 PM ---------- Previous update was at 02:44 PM ----------

Jim the grep in my command interpretes * as everything.

I'm not sure exactly what you're asking, but I suspect you want grep -F or xargs -I .

Nor am I sure what you want to achieve. If you want to grep all BW*2013* files for the results of your previous grep , you could try grep 's -f (optain patterns from file) option using "-" for the file thus pointing at stdin:

grep "ACCOUNT NOT FOUND" BW-CDR-2013* |grep -v INCE|cut -f32 -d"," | sort -u | grep -f - BW*2013*

But the reference to the pattern will be lost. Slightly better might be

grep "ACCOUNT NOT FOUND" BW-CDR-2013* |grep -v INCE|cut -f32 -d"," | sort -u | while read P; do echo "$P"; grep -ch "$P" BW*2013*;  done 

Experiment with grep 's options to adapt to your needs...