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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting finding date numeral from file and check the validity of date format
# 8  
Old 11-17-2011
Code:
echo $name | sed s/[a-z_.]//g

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 11-17-2011 at 07:58 AM.. Reason: Please use code tags, thanks
# 9  
Old 11-17-2011
thanks but still
Code:
how to validate the date when o/p date can be in any format.
no static one .

is there any way to do so??

---------- Post updated at 06:34 AM ---------- Previous update was at 06:32 AM ----------

Quote:
Originally Posted by kondeti_satish
echo $name | sed s/[a-z_.]//g
excellent man.

---------- Post updated at 09:23 AM ---------- Previous update was at 06:34 AM ----------

if name=hvar_rgrpd_10d_hvams17_11-17-2011_kgr_prod.rec
Then none of the trick working .
can anyone come up with better idea to extarct the date field only ???
# 10  
Old 11-17-2011
Try this...
Code:
sed 's/.*\([0-9]\{2\}[- ]*[0-9]\{2\}[- ]*[0-9]\{4\}\).*/\1/g' input_file

--ahamed
# 11  
Old 11-18-2011
If gawk (GNU awk) is avaliable for you ( most linux distros ) , you can do the following:

Code:
# cat file
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
triss_fxcb_20111117.csv.checksum
r_rgrpd_10d_hvams17_11-17-2011_kgr_prod.rec

Code:
# gawk 'match($0,/([0-9]+-*[0-9]+-*[0-9]+)/,a){print a[1]}' file
20111117
20111117
11172011
11-17-2011
20111117
20111117
20111117
11-17-2011

Then is easy to translate the perl code to awk:
Code:
#!/usr/bin/env ksh

# YYYYMMDD,DDMMYYYY,MMDDYYYY or YYYY-MM-DD

validaF ()
{
dateV="${1}"

echo "${dateV}"|gawk  '{
   if (match($0,/^((?:19|20)[0-9][0-9])-*(0[1-9]|1[012])-*(0[1-9]|[12][0-9]|3[01])$/,a)) {
      year=a[1]+0
      mon=a[3]+0
      day=a[4]+0
      }
   else if (match($0,/^(0[1-9]|[12][0-9]|3[01])(0[1-9]|1[012])((19|20)[0-9][0-9])$/,a)) {
      year=a[3]+0
      mon=a[2]+0
      day=a[1]+0
      }
   else if (match($0,/^(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])((19|20)[0-9][0-9])$/,a)) {
      year=a[3]+0
      mon=a[1]+0
      day=a[2]+0
      }
   else {
       print "KO: "$0
       exit
     }

   if (day == 31 && (mon == 4 ||  mon == 6 || mon == 9 || mon == 11)) 
      print "KO: "$0 # 30 days months
   else if (day >= 30 && mon == 2) 
      print "KO: "$0 # Febrary never 30 o 31 
   else if (mon == 2 && day == 29 && ! (  year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)))
      print "KO: "$0 # Febrary  29 leap year
   else  
      print "Correct date !:" $0 
   }'

}


validaF 11082011
validaF 12312010
validaF 13312010
validaF 20110811
validaF 20110841
validaF 2012-02-29
validaF 20110229

Code:
# date.sh
Correct date !:11082011
Correct date !:12312010
KO: 13312010
Correct date !:13312010
Correct date !:20110811
KO: 20110841
Correct date !:20110841
Correct date !:2012-02-29
KO: 20110229

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date: invalid date trying to set Linux date in specific format

i try to set linux date & time in specific format but it keep giving me error Example : date "+%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" or date +"%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" keep giving me this error : date: invalid date ‘19-01-2017 00:05:01' Please use CODE tags... (7 Replies)
Discussion started by: umen
7 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. UNIX for Dummies Questions & Answers

Rename all Files in a UNIX Directory from one date format to another date format

Hi Unix Gurus, I would like to rename several files in a Unix Directory . The filenames can have more than 1 underscore ( _ ) and the last underscore is always followed by a date in the format mmddyyyy. The Extension of the files can be .txt or .pdf or .xls etc and is case insensitive ie... (1 Reply)
Discussion started by: pchegoor
1 Replies

4. Shell Programming and Scripting

How to check if date format is correct?

Hi! how do i know if the input is the same as the required date format? the date should be dd/mm/YYYY ex. 2/3/2012 or 15/11/2012 all the following conditions must return an error: *input of string *day is > 31 or < 1 *month is > 12 or < 1 *year is < 2013 suppose the date format is stored... (1 Reply)
Discussion started by: angilulu
1 Replies

5. Shell Programming and Scripting

Converting a date to friday date and finding Min/Max date

Dear all, I have 2 questions. I have a file with many rows which has date of the format YYYYMMDD. 1. I need to change the date to that weeks friday date(Ex: 20120716(monday) to 20120720). Satuday/Sunday has to be changed to next week friday date too. 2. After converting the date to... (10 Replies)
Discussion started by: 2001.arun
10 Replies

6. Shell Programming and Scripting

Finding files before a certain date with predefined format

Guys, I have an input file such as below I would like to know how i would be able to find items created before 2011-10-01 Appreciate any expert advice. Thanks. (3 Replies)
Discussion started by: aismann
3 Replies

7. Shell Programming and Scripting

finding the previous day date and creating a file with date

Hi guys, I had a scenario... 1. I had to get the previous days date in yyyymmdd format 2. i had to create a file with Date inthe format yyyymmdd.txt format both are different thanks guys in advance.. (4 Replies)
Discussion started by: apple2685
4 Replies

8. Shell Programming and Scripting

Check input date format?

how to check input date format. for example $input_date must be in format dd.mm.gg script is execute like this: bin/script1.sh 14.12.2009 script1.sh code: #!/bin/sh input_date=$1 CMD="/app/si/test/test.sh $input_date" echo "*****" $CMD (2 Replies)
Discussion started by: waso
2 Replies

9. Shell Programming and Scripting

convert date format to mysql date format in log file

I have a comma delimited log file which has the date as MM/DD/YY in the 2nd column, and HH:MM:SS in the 3rd column. I need to change the date format to YYYY-MM-DD and merge it with the the time HH:MM:SS. How will I got about this? Sample input 02/27/09,23:52:31 02/27/09,23:52:52... (3 Replies)
Discussion started by: hazno
3 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