Date validity check


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date validity check
# 8  
Old 11-12-2013
@rbatte1

the above case staement is throwing error...

i need only shell script or perl as mine supports that only.

i should not read line by line beacuse if 5 lakh records are there it will take long time.

i think about a scenario could any one help in getting it in a script..

test.txt - main file

test.txt
=======
Code:
2013-12-12
2013-13-12
2013-12-32
2012-02-29
2012-02-28
2013-12-31
2013-11-31
2013-11-30
2013/12/12
2013-32-12
2013-12-42

1. find first level of format check...in this 2013/12/12 or 2013/23/43 0r 2013-13-32 or 2013-12-43 like this will and be be captured
Code:
grep -v "^[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]$" test.txt | while read line
do
   echo "\t$line" >> vv.txt
done
Header_chk=`cat vv.txt | wc -l`
Header_month_max=`cat test.txt | cut -c6-7 | sort -nr | head -1` ;
Header_month_min=`cat test.txt | cut -c6-7 | sort -nr | tail -1` ;
Header_Day_max=`cat test.txt | cut -c9-10 | sort -nr | head -1` ;
Header_Day_min=`cat test.txt | cut -c9-10 | sort -nr | tail -1` ;
 if [[   "$Header_chk" -ne 0 || "$Header_month_max" -gt "12" || "$Header_Day_max" -gt "31" "$Header_month_min" -eq "00" || "$Header_Day_min" -eq "00" ]]
   then
   echo "N">>vijay.txt
 fi


2.seperate feb into one file
feb file name is now feb.txt
i have to split feb.txt into two files( leap.txt and nonleap.txt)
leap.txt
=======
Code:
Day_max=`cat leap.txt | cut -c9-10 | sort -nr | tail -1` ;
Day_min=`cat leap.txt | cut -c9-10 | sort -nr | tail -1` ;
  if [[   "$Day_max" -gt 28 || "$Day_min" -eq "00"  ]]
   then
   echo "N">>vijay.txt
  fi

nonleap.txt
=======
Code:
Day_max=`cat nonleap.txt | cut -c9-10 | sort -nr | tail -1` ;
Day_min=`cat nonleap.txt | cut -c9-10 | sort -nr | tail -1` ;
  if [[   "$Day_max" -gt 29 || "$Day_min" -eq "00"  ]]
   then
   echo "N">>vijay.txt
  fi

but how to to split the leap year dates and non leap year dates in tow files Smilie

3.seperate APR,june,sep,nov into one file
APR,june,sep,nov file name is now 30day.txt
Code:
Day_max=`cat 30day.txt | cut -c9-10 | sort -nr | tail -1` ;
Day_min=`cat 30day.txt | cut -c9-10 | sort -nr | tail -1` ;
  if [[   "$Day_max" -gt 30 || "$Day_min" -eq "00"  ]]
   then
   echo "N">>vijay.txt
  fi


finally after all validation if the vijay.txt has a N, it means a invalid date has been found in some check ..
finally i will tell the main file is invalid.
please help me out for this scenario by a script.

Last edited by Franklin52; 11-13-2013 at 03:20 AM.. Reason: Please use code tags
# 9  
Old 11-12-2013
Putting code or output into CODE tags makes it a lot more readable. Can you edit your post to put them in please.

You also seem to be persisting with your original costly code which fires up all sorts of processes (cut, cat, sort, etc.). Can you show me the errors you are getting with my offering or why it is unacceptable. I can then try to help.

Reading the unformatted block of text, you seem to like my grep so I'd like to know how I can improve the rest.




Robin
# 10  
Old 11-12-2013
You can do this easily in perl

validate.pl:
Code:
#!/usr/bin/perl
use Time::Local 'timelocal';
use POSIX;

while (my $ln = <STDIN>) {
    ($year, $month, $day) = split(/[-\/]/, $ln);
    eval { timelocal(0,0,0,$day,$month-1,$year) };
    exit 1 if $@;
}
exit 0;

Test file from you shell like this:

Code:
if ./validate.pl < test.txt
then
    echo "File is valid"
else
    echo "File is invalid"
fi

# 11  
Old 11-13-2013
Code:
uapp291n  -> echo "Basic invalid formatted dates found:-"
grep -v "^[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]$" vv.txt | while read line
do
   echo "\t$line"
doneBasic invalid formatted dates found:-
/ddsaa/dd/src_files/dddd/bisslling/ihguapp291n  -> grep -v "^[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]$" vv.txt | while read line
> done
>    echo "\t$line"
> done
        2011/03/01
/ddsaa/dd/src_files/dddd/bisslling/ihguapp291n  -> echo "\nLooking for invalid date values:-"
Looking for invalid date values:-
/ddsaa/dd/src_files/dddd/bisslling/ihguapp291n  -> (IFS=-
> typeset -i d m Y mxd L
grep "^[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]$" vv.txt | while read Y m d
> grep "^[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]$" vv.txt | while read Y m d
> do
>    case $m in
>       1)  mxd=31 ;;
>       2)  ((L=($Y%4)/($Y%4))) 2>/dev/null ; ((mxd=29-$L)) ;; # Handles leap year
ksh: syntax error: `>' unexpected
/ddsaa/dd/src_files/dddd/bisslling/ihguapp291n  ->       3)  mxd=31 ;;
ksh: syntax error: `)' unexpected
/ddsaa/dd/src_files/dddd/bisslling/ihguapp291n  ->       4)  mxd=30 ;;
ksh: syntax error: `)' unexpected
/ddsaa/dd/src_files/dddd/bisslling/ihguapp291n  ->       5)  mxd=31 ;;
ksh: syntax error: `)' unexpected
      6)  mxd=30 ;;
      7)  mxd=31 ;;
/ddsaa/dd/src_files/dddd/bisslling/ihguapp291n  ->       6)  mxd=30 ;;
ksh: syntax error: `)' unexpected
/ddsaa/dd/src_files/dddd/bisslling/ihguapp291n  ->       7)  mxd=31 ;;
ksh: syntax error: `)' unexpected
/ddsaa/dd/src_files/dddd/bisslling/ihguapp291n  ->       8)  mxd=31 ;;
ksh: syntax error: `)' unexpected
/ddsaa/dd/src_files/dddd/bisslling/ihguapp291n  ->       9)  mxd=30 ;;
ksh: syntax error: `)' unexpected
/ddsaa/dd/src_files/dddd/bisslling/ihguapp291n  ->       10) mxd=31 ;;
ksh: syntax error: `)' unexpected
/ddsaa/dd/src_files/dddd/bisslling/ihguapp291n  ->       11) mxd=30 ;;
ksh: syntax error: `)' unexpected
/ddsaa/dd/src_files/dddd/bisslling/ihguapp291n  ->       12) mxd=31 ;;
ksh: syntax error: `)' unexpected
/ddsaa/dd/src_files/dddd/bisslling/ihguapp291n  ->       *)  mxd=0  ;;
ksh: syntax error: `)' unexpected
/ddsaa/dd/src_files/dddd/bisslling/ihguapp291n  ->    esac
ksh: syntax error: `esac' unexpected
/ddsaa/dd/src_files/dddd/bisslling/ihguapp291n  ->    if [ ${d} -gt ${mxd} -o ${d} -lt 1 ]
   then
      echo "\tInvalid date found \"${Y}-${m}-${d}\""
   fi
>    thenne)
>       echo "\tInvalid date found \"${Y}-${m}-${d}\""
>    fi
ksh: test: argument expected
/ddsaa/dd/src_files/dddd/bisslling/ihguapp291n  -> done)
ksh: syntax error: `done' unexpected
/ddsaa/dd/src_files/dddd/bisslling/ihguapp291n  ->


this is the error i am getting

pls help

Last edited by vbe; 11-20-2013 at 11:27 AM..
# 12  
Old 11-13-2013
It would be nice if you wrap the output in CODE tags. It makes it so much easier to read for such little effort. Simply highlight the output or code, then on the toolbar of the little editing window, press the white square that has "co" over "de", between the speech bubble and the page with "php" on it.

Alternatively, add the text [CODE] before the text and [/CODE] afterwards manually.



Complaint over.

Perhaps I should have said that this is a shell script and not to be pasted to the command line. Can you save it all in a file. Let's call it datescan as an example and then do the following on the command line:-
Code:
chmod 755 datescan
./datescan


What output (remember the CODE tags) do you get from that?




Regards,
Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Confirming validity of programming language tools

so i have scripts that get run in ways similar to this: cat script.pl | perl - $1 $2 $3 cat script.rb | ruby - $1 $ 2 $3 my question is, how can i verify that that the "perl" or "ruby" or "python" tool being run on the box is actually a legit tool? meaning, someone may move the tool from... (2 Replies)
Discussion started by: SkySmart
2 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

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

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

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

6. Shell Programming and Scripting

Script to check the validity of password

Hi, I have to validate the passwords for 100s of unix users across several servers. I have the list of unix users and servers with passwrods. How can I check whether a password is correct or not using a single shell script? Note : I do not have root privileges on any server. All the... (1 Reply)
Discussion started by: Pupil
1 Replies

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

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

checking for command output validity

hi, i'm trying to write a script to check if the home directories of users are set correctly. below is an extract of the script here, i am trying to put the name of the owner of the home directory into the variable dirperm (by reading lines in /etc/passwd). however, it seems that when the... (1 Reply)
Discussion started by: roddo90
1 Replies

10. Shell Programming and Scripting

Validity

Hey, I was wondering how I could write a bash script which accepts: cat <<% | bash ./results06 ---------------------------------------------------------- Exam Results 2006 ---------------------------------------------------------- ... (1 Reply)
Discussion started by: Xerobeat
1 Replies
Login or Register to Ask a Question