awk command with if & mail


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command with if & mail
# 1  
Old 04-27-2015
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.

Code:
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.
# 2  
Old 04-27-2015
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
Code:
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; }

This User Gave Thanks to RudiC For This Post:
# 3  
Old 04-30-2015
Hi Rudic, Thanks for your reply, I have tried your suggestion but it is not working.
Below one I have tried .

Code:
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.
Code:
FS="," "$FILE" >TMP || { mail -s "test mail" ${MAIL_LIST} <TMP;rm TMP;}


Kindly suggest.

Thanks & Regards
Kumar
# 4  
Old 04-30-2015
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.

Last edited by RudiC; 05-03-2015 at 04:13 AM.. Reason: FS was set to ",", not ";"
# 5  
Old 04-30-2015
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.

Code:
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

Last edited by dravi_laxmi; 04-30-2015 at 03:41 PM..
# 6  
Old 04-30-2015
You obviously didn't read my last post.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 04-30-2015
Thanks Sir, It is my mistake, It is working now.
Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Script to ZIP/SAVE & then DELETE Log file & send a mail conformation for any error

ENVIROMENT Linux: RHEL 6.4 Log Path: /usr/iplanet/servers/https-company/logs Log Format: user.log.03-15-2015 I have log4j log rotation enabled rotating files on a daily basis. The rotated logs are NOT compressed & are taking up too much space. I need a script that will run daily that... (1 Reply)
Discussion started by: admin_job_admin
1 Replies

2. Shell Programming and Scripting

Different output for awk command on Linux & HP-UX

I am using an awk command to extract a particular portion of a string. Below is the command and its output on a Linux system: oracle@host1:/tmp (/home/oracle) $uname -a Linux host1 2.6.32-279.39.1.el6.x86_64 #1 SMP Fri Nov 15 05:38:26 EST 2013 x86_64 x86_64 x86_64 GNU/Linux ... (7 Replies)
Discussion started by: veeresh_15
7 Replies

3. Homework & Coursework Questions

Creating a function that sends a mail using mail command

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The function will be called m and it will allow you to send an email to someone using the mail command. The... (1 Reply)
Discussion started by: Drucian
1 Replies

4. Shell Programming and Scripting

[Solved] BASH - chaining TEST and COMMAND with && and II

Can you explain what this line of script is doing. What I have understood is : -- variable C is the name of a software which is either not installed, so it must be installed or allready installed and then should be update if newer version found -- branch B="$B $C" is to install the software --... (4 Replies)
Discussion started by: jcdole
4 Replies

5. Shell Programming and Scripting

Problem Using If & For loop in AWK Command

I am parsing file for the fields using awk command, first i check 26th field for two characters using substr function if it matches then using for loop on array i search 184th field for 4 chars if it matches then i print the required fields but on execution i get the error, please help...... (5 Replies)
Discussion started by: siramitsharma
5 Replies

6. Linux

awk filter & Auto gen Mail

hi experts 2012-01-30 10:30:01:812 "y" "NA" "30/01/2012 10:30:01:154 AM" 2012-01-30 10:33:46:342 "y" "NA" "30/01/2012 10:33:45:752 AM" 2012-01-30 10:41:11:148 "n" "200" "30/01/2012 10:41:10:558 AM" 2012-01-30 10:44:48:049 "y" "NA" ... (7 Replies)
Discussion started by: nith_anandan
7 Replies

7. UNIX for Dummies Questions & Answers

AIX: mail command & server

Hi, I have to change the mail server, but I could not find the file where it is written in. I use the following command: </> echo krneki | mail -v -s "New report" receiver@our.comp Server name ("skala") can be seen in a terminal output .... receiver@our.comp ... Connecting to... (0 Replies)
Discussion started by: frajer
0 Replies

8. Solaris

identify the mail server for mail command

Hi , I am new to unix , i am using the mail and mailx command to send the mail .How come i will know the my mail command using which server as mail box.. Please help me .. Thanks in advance (1 Reply)
Discussion started by: julirani
1 Replies

9. UNIX for Dummies Questions & Answers

Problem with xterm & tcsh & sourcing a script in a single command

Hi friends, I have a script that sets the env variable path based on different conditions. Now the new path variable setting should not done in the same terminal or same shell. Only a new terminal or new shell should have the new path env variable set. I am able to do this only as follows: >cd... (1 Reply)
Discussion started by: sowmya005
1 Replies
Login or Register to Ask a Question

Featured Tech Videos