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
# 1  
Old 11-17-2011
finding date numeral from file and check the validity of date format

hi there

I have file names in different format as below

Code:
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

Now my this little piece of awk will give me only date from that file name

Code:
echo $name | awk -F"_" '{  for(i=1;i<=NF;++i) if($i ~ /[[:digit:]]/) print $i}'

if name is triss_20111117_fxcb.csv then o/p 20111117 which is perfect
if name is xpnl_hypo_reu_miplvdone_11-17-2011.csv then o/p is 11-17-2011.csv which is not perfect as .csv
if name is xpnl_hypo_reu_miplvdone_20111117xfb.csv then o/p is 20111117xfb.csv
if name is triss_fxcb_20111117.csv.checksum then o/p is 20111117.csv.checksum

question is how to remove .csv or any charcter from the o/p as I only need the date from the filename ?

and once I have the date in format like
Code:
YYYYMMDD,DDMMYYYY,MMDDYYYY or YYYY-MM-DD

how can i validate these date format are valid date. date can be in any of above form.
e.g.
11-17-2011
20111117
11172011
# 2  
Old 11-17-2011
Try:
Code:
awk -F[._] '{
  for(i=1;i<=NF;++i) {
    s=$i
    if(gsub("[0-9]",x,s)==8){
      gsub("[a-zA-Z]",x,$i) 
      print $i
    }
  }
}' file

# 3  
Old 11-17-2011
If perl is ok, this is the equivalent for the dates:
Code:
 perl -n -e '/(\d+-*\d*-*\d*)/;print $1."\n";' file

And for the validation, you can star with:
Code:
#!/usr/bin/env ksh

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

echo "${fecha}"|perl -n -e '
   if ( m!^((?:19|20)\d\d)(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])$!) {
      # $1 -> anyo  $2 -> mes , $3 -> dia
      if ($3 == 31 and ($2 == 4 or $2 == 6 or $2 == 9 or $2 == 11)) {
         print "0" ; # Meses con 30 dias
         }
      elsif ($3 >= 30 and $2 == 2) {
         print "0" ; # Febrero nunca 30 o 31
         } 
      elsif ($2 == 2 and $3 == 29 and not ($1 % 4 == 0 and ($1 % 100 != 0 or $1 % 400 == 0))) {
         print "0" ; # 29 de Febrero cuando no es bisiesto
         } 
      else {
         print "Correct date !: $_ \n"; # Fecha valida
         }
      } 
    else {
       print "KO: $_" ; # Sin formato de fecha
    }'
}

validaF 20110811
validaF 20110841

Of course you need to adapt the regex to match the rest of the date formats.
# 4  
Old 11-17-2011
thanks KlashXX and Franklin for your suggestion. but unfortunatley I can't use perl in my case .
and I found Frankling sugesstion working for all type of files .

By the mean time I tried below option

Code:
echo $name | awk -F'[^0-9]' '{  for(i=1;i<=NF;++i) if($i ~ /[[:digit:]]/) print $i}'

which is also works for almost all the conditions except if the filename is
xpnl_hypo_reu_miplvdone_11-17-2011.csv i.e if in any characters are in between then o/p is
Code:
11
17
2011

can anyone suggest how to improve this liner.I don't care if any - removed between number but atleast it should be numeral in one line not line after another.

anyway can anyone suggest how to validate the date o/p please??
# 5  
Old 11-17-2011
Try:
Code:
awk -F'[^0-9-]' '{  for(i=1;i<=NF;++i) if($i ~ /[[:digit:]]/) print $i}'

# 6  
Old 11-17-2011
you are star man.
Now can you assist me how to validate those o/p as correct date format.
My date can be in anyform from the below .

Code:
YYYYMMDD,DDMMYYYY,MMDDYYYY,MM-DD-YYYY or YYYY-MM-DD

# 7  
Old 11-17-2011
use printf instead of print

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

Featured Tech Videos