Script pick elements according to the date in file

Hello,

i am having file details having elements mentioned below

Asia jan 18
Europe Jan 19
America Jan 20

I made script but it picks all the elements, i want to pick the elements according to the date to run the script i made cron job.

Please advise how it can work....

mon=$( date +"%b" )
dd=$( date +"%d" )

awk -v M=$mon -v D=$dd '$2==M&&$3==D{ print }' filename

Thanks Bipin for prompt reply but it didn't worked can you please provide code based on below example

I will be making cron job which will be running every morning at 9:00 AM and i want to run the script only on the elements according to the date,

if there is no element on the specified date than the scipt should not run....

Asia   Jan 17
Europe Jan 19
North America Jan 20
South America Jan 22

This is pretty much straightforward problem. I recommend always searching this forum or internet and put some effort by yourself before posting such questions.

Here is the code that will check if an entry with current date exists. If no, then exit:

DT=$( date +"%b %d" )
[[ $( grep -c "$DT" filename ) -eq 0 ]] && exit 1

Thanks Bipin.

---------- Post updated at 03:55 PM ---------- Previous update was at 02:26 PM ----------

I tried the code but it didnt worked and searched the forum also didnt find the solution than i tired one command mentioned below but this also not giving the proper output.

grep -c "date +"%d-%b"" File.txt
0
cat File.txt
Asia 16-Jan
Europe 17-Jan
America 18-Jan

output should only show Asia......

Please advise if there is some existing forum on this issue.

That is not going to work. You cannot put a command just inside double quote and run it.

Use sub-shell:-

grep "$( date +"%d-%b" )" File.txt

Or use back-ticks:-

grep "`date +"%d-%b"`"  File.txt

I recommend using the sub-shell.

Thanks Bipin second command worked and showed output but first one showed error.....

grep  "$(date +"%d-%b")" File.txt
Illegal variable name.

Remove the double quotes inside:-

grep "$( date +%d-%b )" File.txt
grep "$(date +%d-%b)" rnc.txt 

Output
Illegal variable name.

 
a=`date| cut -c1-6`
grep "$a" filename

rajjev_saini123, what SHELL & OS?

echo $SHELL
uname

I have written the complete code now it shows syntax erro

CWD=/home/test/Bench
cd $CWD
(grep "`date +%d-%b"`" File.txt) >  /home/test/Bench/dateout
if [ -s / home/test/Bench/dateout ]
then
echo �data�  > /home/test/ Bench /test
else
echo "File Empty"
cat /home/test/ Bench /test > /home/test/ Bench /resultest
awk '!/get/' resultest > /home/test/ Bench/output
awk '!/0x000/' resultest > /home/test/Bench/output1
if [ -s /home/test/ Bench/output1 ]
then
MAILTO="raj@yahoo.co.in"
CONTENT="/home/test/ Bench /output1"
(
echo "Subject: Test"
echo "MIME-Version: 1.0"
echo "Content-Type: text/plain"
echo "Content-Disposition: inline"
cat $CONTENT
) | /usr/sbin/sendmail -t $MAILTO
fi
else
echo "bbb"
fi 

SunOS
/usr/bin/tcsh

Ok, will not work in tcsh.