Check date and do action


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check date and do action
# 1  
Old 11-01-2012
Check date and do action

I would like to write a script to do such thing , if a directory have file that the extension is today (yymmdd), then do a specific action , I will run a cron job to run this script .

For example

Today is 2nd , if the directory have files which file name are abc.121102 and def.121102 , then echo the file name , so the output is abc , def

tomorrow is 3rd , if the directory have files which the file name are xxx.121103 and yyy.121103 , then echo the file name , so the output is xxx , yyy

can advise how to it ?

thanks.
# 2  
Old 11-01-2012
What shell are you running? bash has the simplest way to do this.
# 3  
Old 11-01-2012
Code:
TODAY=`date +"%y%m%d"`

if [ `ls -l *.${TODAY} 2> /dev/null | wc -l` -ne 0 ]
then
        for file in *.${TODAY}
        do
                echo ${file} | awk -F"." ' { print $1 } '
        done
else
        echo "No files found."
fi

# 4  
Old 11-02-2012
Quote:
Originally Posted by bipinajith
Code:
TODAY=`date +"%y%m%d"`
 
if [ `ls -l *.${TODAY} 2> /dev/null | wc -l` -ne 0 ]
then
        for file in *.${TODAY}
        do
                echo ${file} | awk -F"." ' { print $1 } '
        done
else
        echo "No files found."
fi

thx reply ,
it is a very good script .
if the file name is not abc.121102 , it is abc.program.121102 , what can I do ?

other file name is
def.program.121102
xxx.program.121102
yyy.program.121102

thanks.
# 5  
Old 11-02-2012
Replace

Code:
echo ${file} | awk -F"." ' { print $1 } '

with

Code:
echo ${file} | sed 's/\.'$TODAY'//g'

It will work for abc.121102 or abc.program.121102
# 6  
Old 11-06-2012
Thank you for reply and I have one more requirements , if I want to echo the first delimitor ( def , xxx , yyy ) of the file name , that mean when run the script , it display def , xxx ,yyy , what can i do ?

Thanks.

Last edited by ust3; 11-09-2012 at 04:55 AM..
# 7  
Old 11-09-2012
is it possible to do that ?

thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check load and action

Hey everyone can you check this script logic ? it has to restart webservice if found server load is higher than X, also i have put it in crontab to run every one minute #!/bin/bash loadavg=$(uptime | awk -F "." '{ print $1 }' | awk -F ":" '{ print $5 }') if ; then pkill -9 httpd service... (7 Replies)
Discussion started by: nimafire
7 Replies

2. Shell Programming and Scripting

Check, if date is not today

hello, in a file exist entries in date format YYYYMMDD. i want to find out, if there are dates, which isn't today's date. file: date example text 20140714 <= not today's date 20140715 <= not today's date 20140716 <= today's date my idea is to use Perderabo's datecalc ... (2 Replies)
Discussion started by: bora99
2 Replies

3. Shell Programming and Scripting

Date validity check

hi All, i have file in which it has 2000 records like, test.txt ==== 2011-03-01 2011-03-01 2011-03-01 2011-03-01 2011-03-01 2011-03-02 2011/03/02 previously i used for loop to find the date check like below, for i in `cat test.txt` do d=`echo $i | cut -c9-10| sed 's/^0*//'`;... (11 Replies)
Discussion started by: mechvijays
11 Replies

4. Shell Programming and Scripting

Perl code to check date and check files in particular dir

Hi Experts, I am checking how to get day in Perl. If it is “Monday” I need to process…below is the pseudo code. Can you please prove the code for below condition. if (today=="Monday" ) { while (current_time LESS THAN 9:01 AM) ... (1 Reply)
Discussion started by: ajaypatil_am
1 Replies

5. Shell Programming and Scripting

Check if a date field has date or timestamp or date&timestamp

Hi, In a field, I should receive the date with time stamp in a particular field. But sometimes the vendor sends just the date or the timestamp or correctl the date&timestamp. I have to figure out the the data is a date or time stamp or date&timestamp. If it is date then append "<space>00:00:00"... (1 Reply)
Discussion started by: machomaddy
1 Replies

6. Shell Programming and Scripting

finding date numeral from file and check the validity of date format

hi there I have file names in different format as below triss_20111117_fxcb.csv triss_fxcb_20111117.csv xpnl_hypo_reu_miplvdone_11172011.csv xpnl_hypo_reu_miplvdone_11-17-2011.csv xpnl_hypo_reu_miplvdone_20111117.csv xpnl_hypo_reu_miplvdone_20111117xfb.csv... (10 Replies)
Discussion started by: manas_ranjan
10 Replies

7. UNIX for Dummies Questions & Answers

Need to check date at the end

Hi, The file contains date at the end, need to check if it is today's date or not. How to check? Any help. Regards, Venkat. (1 Reply)
Discussion started by: venkatesht
1 Replies

8. Shell Programming and Scripting

Check for date

How to validate the first line from 1-8 position of audit file that contains the script run date... script could run in random dates. head -1 file1 20090516 100034837SHDHSHE (9 Replies)
Discussion started by: ford2020
9 Replies

9. Shell Programming and Scripting

How to check date variable according to the current date?

Hi folks, I need to write a script that should activate a process according to the current hour. The process should be activatet only if the hour is between midnight (00:00) and 07:00. How should I create the condition? Thanks in advance, Nir (2 Replies)
Discussion started by: nir_s
2 Replies

10. Shell Programming and Scripting

Check the format of Date

Hi, I have a date field in my input file. I just want to check if its in the format "DD-MM-YYYY". Is there any command which can achieve this? Thanks and Regards, Abhishek (2 Replies)
Discussion started by: AAA
2 Replies
Login or Register to Ask a Question