I have a select query which runs and generate the count. It exports the Answer in a text file which has below records eg.:

    Unprocessed record for A Sources
----------------------Unprocessed record count for ABC SOURCE ------------------
abc_count=            0
--------------------Unprocessed record count for CDE Source --------
CDE_count=            0
-----------------------Unprocessed record count for DEF Source -----------------
DEF_count=            0
------------------Unprocessed record count for GHI Source ----------------------
GHI_count=            56

I want to write a Unix command which checks if for above source any of the count is not equal to 0 if it is not equal to 0 then mail it to the required team.

  • mail it to the required team - what required team? what should be mailed, the whole record? – RomanPerekhrest Aug 21 '17 at 13:12
  • Required team means Users. if any of the record count is not equal to 0 then mail the whole record . – Vinayak Das Aug 21 '17 at 13:28

Could use the grep to get output of the wanted process
awk the second collumn and test if its zero
then echo the message you want to mail or mailx with the needed subject and adresses.
You could iterate over multiple predifine strings

Below answer used based on exactly the output as given in question and assume that furthe checks will have the same pattern output.

for CHECK in $(awk '{print $1}' output.txt | grep count=| cut -d"=" -f1)
if [ $(grep $CHECK output.txt | awk '{print $2}') -ne 0 ]
echo "$(grep $CHECK output.txt")" | mailx -s "CHECK ABC PROCESS" foo@bar.com
  • 1
    GiannakopoulosJ Thanks for replying....getting some syntax error though syntax error at line 9 : `"' unmatched – Vinayak Das Aug 21 '17 at 15:18
  • missed a ) at echo echo "$(grep $CHECK output.txt" | mailx -s "CHECK ABC PROCESS" foo@bar.com. I have already added it. – igiannak Aug 21 '17 at 15:29
  • I hope i am using the brackets correctly for CHECK in $(awk '{print $1}' hello | grep count=| cut -d"=" -f1) do if [ $(grep $CHECK output.txt | awk '{print $2}') -ne 0 ] then echo "$(grep $CHECK output.txt)" | mailx -s "CHECK ABC PROCESS" foo@bar.com fi done still getting below error :-----awk: can't open hello – Vinayak Das Aug 21 '17 at 15:40
  • as it is but change output.txt with your output file. error awk: can't open hello try to find a hello file. in your case i don't know the file. adjust the script in your output file needs. – igiannak Aug 21 '17 at 15:43
  • 1
    I have some negative reaords as well. what if i dont want records which are not eqaul to zero which was implemeted previously and also records which are negative.can i use -gt instead of -ne – Vinayak Das Aug 23 '17 at 15:37
 awk '/[[:digit:]]/ { if ($2 == 0) { system("echo \""$0"\" | mailx -s \"Check Count\" someone@someserver.com") } }' countfile

You can use awk and check for all lines with digits. Then use awk's system function to send emails.

DISCLAIMER - The risks associated with command injection need to be judged before implementing such a solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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