awk command with if & mail

Hi Experts,
I want to check in table that if third column value is "bhu" & if it is greater than 500 it will send mail
other wise there will be no mail. but as per my script mails are still coming even value is below 500 with one blank mail(without any content) with subject line"test mail".
I want that if value is below 500 there will be no mail, if value is above or equal to 500 there will be mail only.

LOGDIR=/file/logs/
export MAIL_LIST=shiva@gmail.com
FILE="$(find $LOGDIR -name "*.txt" -type f|sort -r|head -n1)"
var=$(
awk '($3 == "bhu") && (($10+0)>=500) { print $3" is running in "$1" from last "$10" secs"; }' "$FILE"
)
awk '{
if($3 == "bhu" &&  (($10+0)>=500))}' "$FILE"
echo $var|mail -s "test mail" ${MAIL_LIST}

Please help.

Both your spec and your code snippet are a bit hard to follow.
Looks like you are assigning an empty string to var if $10 is below 500, and then sending that empty value by mail.
In order to send mail only if your conditions are true, try

awk '$3 == "bhu" && ($10+0)>=500 { print $3" is running in "$1" from last "$10" secs"; exit 1}' FS="," "$FILE" >TMP || { mail -s "test mail" ${MAIL_LIST} <TMP; rm TMP; }
1 Like

Hi Rudic, Thanks for your reply, I have tried your suggestion but it is not working.
Below one I have tried .

LOGDIR=/file/logs/
export MAIL_LIST=shiva@gmail.com
FILE="$(find $LOGDIR -name "*.txt" -type f|sort -r|head -n1)"
awk '$3 == "SPO" && ($10+0)>=500 { print $3" is running in "$1" from last "$10" secs"; exit 1}' 
FS="," "$FILE" >TMP || { mail -s "test mail" ${MAIL_LIST} <TMP;rm TMP;}

Actually I want to send mail if $10 is equal or above than 500 then mail will be $3" is running in "$1" from last "$10" secs" and if it is below 500 there will be no mail.

Just could you please also describe this command also , that what will be the mail content.

FS="," "$FILE" >TMP || { mail -s "test mail" ${MAIL_LIST} <TMP;rm TMP;}

Kindly suggest.

Thanks & Regards
Kumar

WHAT is not working? It worked for me...

You can't chop the compound command in the middle of the line; FS="," sets the field separator for awk and needs to follow the awk immediately, as does the rest.
The mail content will be TMP 's content, which is awk 's output.

Hi Rudic,
as per my last post just I want to add that I have checked again with your command
it is continuously running only, after several time I have just forcely stopped with ctrl+D .but it is not giving any output. Kindly suggest.
Please help.

LOGDIR=/file/logs/ 
export MAIL_LIST=shiva@gmail.com 
FILE="$(find $LOGDIR -name "*.txt" -type f|sort -r|head -n1)"
awk '$3 == "SPO" && ($10+0)>=500 { print $3" is running in "$1" from last "$10" secs"; exit 1}'
FS="," "$FILE" >TMP || { mail -s "test mail" ${MAIL_LIST} <TMP;rm TMP;}

Thanks & Regards
Kumar

You obviously didn't read my last post.

1 Like

Thanks Sir, It is my mistake, It is working now.

LOGDIR=/file/logs/  export MAIL_LIST=shiva@gmail.com  FILE="$(find $LOGDIR -name "*.txt" -type f|sort -r|head -n1)" awk '$3 == "SPO" && ($10+0)==0 { print $3" is running in "$1" from last "$10" secs"; exit 1}' FS="," "$FILE" >TMP || { mail -s "test mail" ${MAIL_LIST} <TMP;rm TMP;}

Just one more doubt , I have just tried the another option with above script like if $10=0 then mail should come, after running this script mails are coming but there is no content in the mail, only subject line is there (test mail).

Could you please give some your valuable suggestion for this.

Thanks & Regards,
Kumar

---------- Post updated at 01:49 PM ---------- Previous update was at 01:19 PM ----------

Rudic Sir, just my last updated post that $10==0 is not working, is the reason like there are many rows in table in which $10 is 0 , so is it like that it will not work if condition is fulfill for more than one rows. actually mails are coming without any content but with subject line"test mail"

Regards,
Kumar

Please post a - reasonably truncated - representative sample of your input file.

Hi Rudic Sir,

Thanks for your continuous support, Now again this is not giving any output or mail even though it met with condition .
I have attached the input file inputfile.txt (4.6 KB).
I have tried the below script, I am trying to search BHU file which is in column 3 & also the value which is in column 10 greater or equal than 500 in inputfile.txt file.
But there is no putput or mail is coming. I have also tried to print $TMP & $FILE file also , it is giving the $file output but $TMP there is no value.
I am very new to scripting , please help me.

LOGDIR=/file/logs/
export MAIL_LIST=shiva@gmail.com
FILE="$(find $LOGDIR -name "*.txt" -type f|sort -r|head -n1)"
awk '$3 == "BHU" && ($10+0)>= 500 { print $3" is running in "$1" from last "$10" secs"; exit 1}' FS="," "$FILE" >TMP || { mail -s "test mail" ${MAIL_LIST} <TMP; }
print $TMP
print $FILE

OK, try by dropping the FS=",". Now you have supplied the sample I can see that space (the default) is the field separator.

Hi Rudic Sir,

I have tried what you have suggested, but it is mailing only first row of all the rows which are having more than 500 values.
means if in input file if there are 5 BTC which are having above 500 , it is mailing only first row ,other rows are not coming in mail.

#!/bin/ksh
LOGDIR=file/logs
export MAIL_LIST=shyam@gmail.com
FILE="$(find $LOGDIR -name "*.txt" -type f|sort -r|head -n1)"
awk '($3 == "BTC") && (($10+0) >=500) {print $3" is running in "$1" from last "$10" secs";exit 1}' "$FILE" > TMP || { mail -s "test mail" ${MAIL_LIST} <TMP; rm TMP; }

Then remove the ;exit shown in red above.

It's not quite that easy; we need to transport the FOUND to the END section:

awk '($3 == "BHU") && (($10+0) >=500) {print $3" is running in "$1" from last "$10" secs"; FOUND=1} END {exit FOUND}' /tmp/inputfile.txt > TMP || { mail -s "test mail" ${MAIL_LIST} <TMP; rm TMP; }

We could of course now flip the logics to run mail:

awk '($3 == "BHU") && (($10+0) >=500) {print $3" is running in "$1" from last "$10" secs"; FOUND=1} END {exit 1-FOUND}' /tmp/inputfile.txt > TMP && { mail -s "test mail" ${MAIL_LIST} <TMP; rm TMP; }
1 Like