[DATE] Pattern matching notation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [DATE] Pattern matching notation
# 1  
Old 03-09-2013
[DATE] Pattern matching notation

Hello,
I want to verify the format date like 2013-03-08 (YYYY-MM-DD)
It doesn't work because the pattern matching notation below returns false while the date is right.
Can you help me ? Thanks in advance

Code:
case "$6" in ([1-9][0-9][0-9][0-9]-0[1-9]-0[1-9] | [1-9][0-9][0-9][0-9]-1[0-2]-1[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-2[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-3[0-1]) 
# Nothing, OK ! 
    ;; 
(*)   echo 'Fatal, $6 eq '"'$6'"', Right format is YYYY-MM-DD' >&2 
    ;; 
esac

# 2  
Old 03-09-2013
Even if you fix this pattern, I don't think it will be reliable because this will not cover conditions like leap year.

So If you have GNU date, you can simply do:
Code:
[[ "$( date -d "$1" +%F 2>&1 | grep invalid )" = "" ]] && echo "$1 is valid" || echo "$1 is invalid"

# 3  
Old 03-09-2013
It's OK, I have forgotten some cases

Code:
case "$6" in
([1-9][0-9][0-9][0-9]-0[1-9]-0[1-9] | [1-9][0-9][0-9][0-9]-0[1-9]-1[0-9] | [1-9][0-9][0-9][0-9]-0[1-9]-2[0-9] | [1-9][0-9][0-9][0-9]-0[1-9]-3[0-1] | [1-9][0-9][0-9][0-9]-1[0-2]-0[1-9] | [1-9][0-9][0-9][0-9]-1[0-2]-1[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-2[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-3[0-1])
# nothing, OK !
  ;;
(*)
  echo 'Fatal, $6 = '"'$6'"', Date non conforme OU absente' >&2
  #exit 1
  ;;
esac

---------- Post updated at 02:13 AM ---------- Previous update was at 02:13 AM ----------

Quote:
Originally Posted by bipinajith
Even if you fix this pattern, I don't think it will be reliable because this will not cover conditions like leap year.

So If you have GNU date, you can simply do:
Code:
[[ "$( date -d "$1" +%F 2>&1 | grep invalid )" = "" ]] && echo "$1 is valid" || echo "$1 is invalid"

Thank you for this alternative Smilie

Last edited by amazigh42; 03-09-2013 at 05:35 PM..
# 4  
Old 03-11-2013
I would like to simplify this pattern notation
Code:
([1-9][0-9][0-9][0-9]-0[1-9]-0[1-9] | [1-9][0-9][0-9][0-9]-0[1-9]-1[0-9] | [1-9][0-9][0-9][0-9]-0[1-9]-2[0-9] | [1-9][0-9][0-9][0-9]-0[1-9]-3[0-1] | [1-9][0-9][0-9][0-9]-1[0-2]-0[1-9] | [1-9][0-9][0-9][0-9]-1[0-2]-1[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-2[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-3[0-1])

with that
Code:
([1-9][0-9][0-9][0-9]-[0(1-9) | 1(0-2)]-[0(1-9) | 1(0-9) | 2(0-9) | 3(0-1)])

But it doesn't work.

Is this notation
Code:
 [0(1-9) | 1(0-2)]

means
(01 10) (01 11) (01 12)....(09 10) (09 11) (09 12)....(09 12)


Thank you

---------- Post updated 11-03-13 at 01:13 AM ---------- Previous update was 10-03-13 at 04:14 AM ----------

Hello,
Can anyone help me ?
I wanted to simplfy this first pattern with the second, but it doesn't work.

Code:
([1-9][0-9][0-9][0-9]-0[1-9]-0[1-9] | [1-9][0-9][0-9][0-9]-0[1-9]-1[0-9] | [1-9][0-9][0-9][0-9]-0[1-9]-2[0-9] | [1-9][0-9][0-9][0-9]-0[1-9]-3[0-1] | [1-9][0-9][0-9][0-9]-1[0-2]-0[1-9] | [1-9][0-9][0-9][0-9]-1[0-2]-1[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-2[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-3[0-1])

([1-9][0-9][0-9][0-9]-[0(1-9) | 1(0-2)]-[0(1-9) | 1(0-9) | 2(0-9) | 3(0-1)])

Thanks in advance
# 5  
Old 03-11-2013
How about perl

Code:
 
#!/usr/bin/perl

use strict;
use Time::Local;
my $dt="2013-12-01";
my ($year,$month,$date)=unpack("A4 x1 A2 x1 A2",$dt);
eval  {
        timelocal(0,0,0,$date,$month-1,$year-1900);
        print "Correct date\n";
} or die "Date is not correct $@\n";

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

2. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

3. Shell Programming and Scripting

Perl: scientific notation to decimal notation

hello folks, I have few values in a log which are in scientific notation. I am trying to convert into actual decimal format or integer but couldn't able to convert. Values in scientific notation: 1.1662986666666665E-4 2.0946799999999998E-4 3.0741333333333333E-6 5.599999999999999E-7... (2 Replies)
Discussion started by: scriptscript
2 Replies

4. Shell Programming and Scripting

Pattern matching notation

Hello I have two kinds of logs like server.logserver.log.2013-07-27.001i want to create a variable which look like this (with a pipe) log_name=server.(log|log.$YYYY-MM-DD.)But i tried many cases but it didn't work. Is it possible ? If yes, can you help me. (6 Replies)
Discussion started by: amazigh42
6 Replies

5. Shell Programming and Scripting

Pattern matching notation

Hello, I want to simplify two commands into one. 1st command $type_log_$instance.log.$date.0012nd command $type_log.log.$date.tar.gzInto blue brackets, How do I do to replace the pattern by a blank or _$instance ? $type_log?_$instance].log.$date.*Thank you. (13 Replies)
Discussion started by: amazigh42
13 Replies

6. Shell Programming and Scripting

Pattern matching notation

Hello, I want to match this patterns IS01ORA IS02ORA ... ... IS08ORA With that : ISORA But it doesn't work, can you correct it ? Thanks (8 Replies)
Discussion started by: amazigh42
8 Replies

7. Shell Programming and Scripting

Convert decimal notation to ANSI point code notation

wondering if anyone has any thoughts to convert the below thru a shell script Convert decimal signalling point notation to ANSI point code notation There is a site that does that conversion but i need to implement the solution in a shell script.....Thoughts.... OS: Solaris 9 ... (4 Replies)
Discussion started by: aavam
4 Replies

8. Shell Programming and Scripting

pattern matching lines using the date, and then joining the lines

Hi Guys, Was trying to attempt the below using awk and sed, have no luck so far, so any help would be appreciated. Current Text File: The first line has got an "\n", and the second line has got spaces/tabs then the word and "\n" TIME SERVER/CLIENT TEXT... (6 Replies)
Discussion started by: eo29
6 Replies

9. Shell Programming and Scripting

counting the lines matching a pattern, in between two pattern, and generate a tab

Hi all, I'm looking for some help. I have a file (very long) that is organized like below: >Cluster 0 0 283nt, >01_FRYJ6ZM12HMXZS... at +/99% 1 279nt, >01_FRYJ6ZM12HN12A... at +/99% 2 281nt, >01_FRYJ6ZM12HM4TS... at +/99% 3 283nt, >01_FRYJ6ZM12HM946... at +/99% 4 279nt,... (4 Replies)
Discussion started by: d.chauliac
4 Replies

10. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies
Login or Register to Ask a Question