LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-01-2010, 03:02 AM   #1
saurabhmehan
Member
 
Registered: Jul 2010
Posts: 44

Rep: Reputation: 0
Question want to send mail if all values in second field are zero


In my file there are 2 fields separated by space.
Sample content of file are as follows:
56060 1
54242 0
53030 0

Now i want to check second field of the file and if all values in second field are 0(zero) and send mail containing all contents of the file
 
Old 09-01-2010, 03:12 AM   #2
xeleema
Member
 
Registered: Aug 2005
Location: D.i.t.h.o, Texas
Distribution: Slackware 13.x, rhel3/5, Solaris 8-10(sparc), HP-UX 11.x (pa-risc)
Posts: 988
Blog Entries: 4

Rep: Reputation: 254Reputation: 254Reputation: 254
Greetingz!

Gee, this sure looks like a homework assignment...what is that data from?
As for "how to do it", you can utilize for-loops and if-then statemens within the Korn shell.

Code:
#! /usr/bin/ksh
export FILE_TO_CHECK=/absolute/path/to/the/filename
export ZERO_IN_FIELD_TWO_PRESENT=0
printf "Scanning $FILE_TO_CHECK\n"
for x in $(awk '{print $2}' ${FILENAME_TO_CHECK} )
do
  if [ $x -eq 0 ]; then
    printf "There is a zero in field two. File will be emailed out. Continuing scan...\n"
    export ZERO_IN_FEILD_TWO_PRESENT=1
  fi
done
printf "Scan of $FILE_TO_CHECK completed.\n"
if [ $ZERO_IN_FIELD_TWO_PRESENT -eq 1 ]; then
   mailx -s "subject line you want in the email" email@address.to.mail.to
   printf "$FILE_TO_CHECK has been emailed.\n"
fi
# End of File.

Last edited by xeleema; 09-01-2010 at 03:14 AM. Reason: needed the code tags
 
Old 09-01-2010, 03:28 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

And a bash/awk solution:
Code:
#!/bin/bash

inFile=/path/to/infile

allZeroCheck=$(awk 'BEGIN { allzero = "true" }
{ if ( $2 != "0" ) { allzero = "false" ; exit } }
END { print allzero }' $inFile )

if [[ "$allZeroCheck" == "true" ]]
then
   cat $inFile | mailx -s "subject goes here" x@y.z
fi
Hope this helps.
 
1 members found this post helpful.
Old 09-01-2010, 06:53 AM   #4
r_s
Member
 
Registered: May 2009
Distribution: slackware, fedora, ubuntu
Posts: 101

Rep: Reputation: 21
way easy to do in C/C++ or python, if language is not the restriction.
 
0 members found this post helpful.
  


Reply

Tags
asap



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Equivalence classes, based on field values and multi-key hashtable openSauce Programming 5 05-16-2010 08:35 PM
how to grep for only the values of a specific field hchoonbeng Linux - Newbie 3 11-19-2008 08:20 AM
Postfix : mail cannot send to send outside ( can send/receive locally) bobbinsupport Linux - Networking 3 12-15-2007 10:40 PM
with the linux proxi, backend pop3 mail client can not receive mail and send mail rubin823 Linux - Networking 0 12-21-2006 10:06 PM
storing multiple values within one field in mysql antken Programming 8 12-15-2002 10:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration