Check for date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check for date
# 1  
Old 05-15-2009
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
# 2  
Old 05-15-2009
Quote:
Originally Posted by ford2020
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....
Here's one way to do it:

Code:
$ 
$ cat file1
20090516 100034837SHDHSHE
second line              
third line               
$                        
$ head -1 file1 | perl -MDate::Calc -ne '{
>   ($y,$m,$d)=unpack"a4 a2 a2";
>   if (Date::Calc::check_date($y,$m,$d)) {print "Valid date\n"}
>   else {print "Invalid date\n"}}'
Valid date
$
$
$ cat file2
20090229 100034837SHDHSHE
second line
third line
$
$ head -1 file2 | perl -MDate::Calc -ne '{
>   ($y,$m,$d)=unpack"a4 a2 a2";
>   if (Date::Calc::check_date($y,$m,$d)) {print "Valid date\n"}
>   else {print "Invalid date\n"}}'
Invalid date
$
$

HTH,
tyler_durden
# 3  
Old 05-16-2009
Thanks but I needed by doing UNIX shell scripting .....
# 4  
Old 05-16-2009
is there anyway we can do it in shell scripting?
# 5  
Old 05-16-2009

Code:
## The date-funcs library of shell functions is available at
## http://cfaj.freeshell.org/shell/ssr/08-The-Dating-Game.shtml
. date-funcs

read date junk < "$FILE"

[ ${#date} -ne 8 ] && { echo invalid date; exit 1; }

year=${date%????}
md=${date#????}
month=${md%??}
day=${md#??}

valid_date $year-$month-$day && echo VALID || echo INVALID

# 6  
Old 05-17-2009
Code:
awk 'NR==1{
    year=substr($1,1,4)
    mth=substr($1,5,2)
    day=substr($1,7,2)
    cmd = "cal "mth" "year
    while ( cmd | getline result){
        m=split(result,a," ")
        for (i=1;i<=m;i++){      
            if (a[i] == day){
                print "Date ok"
                break
            }
        }
    }
    value=close(cmd)
    if (value!=0){
        print "Date not ok"
    }
}' file

output
Code:
# more file
20090432 100034837SHDHSHE
some lines
....
# ./test.sh
Date not ok

# more file
20090416 100034837SHDHSHE
some lines
....
# ./test.sh
Date ok


Last edited by ghostdog74; 05-17-2009 at 04:02 AM..
# 7  
Old 05-17-2009
Question

My requirment is like whenever ./test.sh run, i need to compare the date value in output file with script run date. Script can run at any day but once in a day.

Ex:

when test.sh is executed on 16/04/2009, then i need to create shell script to verify the o/p file has script run date.

20090416 100034837SHDHSHE
some lines
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. Shell Programming and Scripting

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 ,... (6 Replies)
Discussion started by: ust3
6 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

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

9. 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

10. Shell Programming and Scripting

check file date in one week ???

How do you write a code in ksh ?? Enter the start date: 20060228 d0 = 20060228; 2. Check for 7 days of report list 1 day after the d0 d1 = 20060301 d2 = 20060302 d3 = 20060303 d4 = 20060304 d5 = 20060305 d6 = 20060306 then cat d0.log d1.log d2.log d3.log d4.log d5.log d6.log >... (1 Reply)
Discussion started by: sabercats
1 Replies
Login or Register to Ask a Question