Pattern matching notation | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Pattern matching notation

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-25-2013
amazigh42 amazigh42 is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 23 January 2014, 3:59 AM EST
Posts: 77
Thanks: 11
Thanked 0 Times in 0 Posts
Pattern matching notation

Hello,

I want to match this patterns
IS01ORA
IS02ORA
...
...
IS08ORA

With that :


Code:
IS[1-8]ORA

But it doesn't work, can you correct it ?

Thanks
Sponsored Links
    #2  
Old 03-25-2013
hanson44 hanson44 is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 12 May 2013, 11:33 PM EDT
Posts: 858
Thanks: 18
Thanked 180 Times in 177 Posts

Code:
IS0[1-8]ORA

[1-8] just matches a single character. To match the '0', you have to explicitly include it.
Sponsored Links
    #3  
Old 03-25-2013
amazigh42 amazigh42 is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 23 January 2014, 3:59 AM EST
Posts: 77
Thanks: 11
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by hanson44 View Post
Code:
IS0[1-8]ORA

[1-8] just matches a single character. To match the '0', you have to explicitly include it.
I have forgotten the zero, i meant

Code:
IS0[1-8]ORA

But the matching doesn't work ?

Thanks for your help.
    #4  
Old 03-25-2013
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 17 September 2014, 3:10 AM EDT
Location: Bangalore,India
Posts: 2,089
Thanks: 12
Thanked 131 Times in 126 Posts
Can you show your complete command?
Sponsored Links
    #5  
Old 03-25-2013
amazigh42 amazigh42 is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 23 January 2014, 3:59 AM EST
Posts: 77
Thanks: 11
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by anbu23 View Post
Can you show your complete command?
In magenta


Code:
#!/bin/bash
#########################################################################
valid() {
case "$1" in
        [E,F,Q,P][A,C,S][A,I,V,P][D,R,T,V]) ;;
        *) return 1 ;;
        esac

        case "$2" in
        [PU][IPS][AGS][1-8]) ;;
        IS0[1-8]ORA ;;
        *) return 1 ;;
        esac

        case "$3" in
        [0-1][0-9]) ;;
        2[0-3]) ;;
        *) return 1 ;;
        esac

        case "$4" in
        [0-5][0-9]) ;;
        *) return 1 ;;
        esac

        case "$5" in
        server) ;;
        exploitation) ;;
        *) return 1 ;;
        esac

        OLDIFS="$IFS"
        IFS="-"
               set -- $6 # $6=YYYY, $2=MM, $3=DD
        IFS="$OLDIFS"

        case "$1" in
        [2-9][0-9][0-9][0-9]) ;;
        *) return 1 ;;
        esac

        case "$2" in
        0[1-9]) ;;
        1[0-2]) ;;
        *) return 1 ;;
        esac

        case "$3" in
        0[1-9]) ;;
        [1-2][0-9]) ;;
        3[0-1]) ;;
        *) return 1 ;;
        esac

        return 0
}
process() {
echo "$1 $2 $3 $4 $5 $6"
}

        if ! valid $1 $2 $3 $4 $5 $6 # Ignore invalid lines
        then
                echo "$1 $2 $3 $4 $5 $6 is invalid"
        else
        process $1 $2 $3 $4 $5 $6
fi


Last edited by amazigh42; 03-25-2013 at 08:42 AM..
Sponsored Links
    #6  
Old 03-25-2013
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 974 Times in 790 Posts
You omitted the ")" after the pattern.

On an unrelated note, in case you are not aware, the following can be re-written ...
Quote:
Originally Posted by amazigh42 View Post

Code:
        case "$3" in
        0[1-9]) ;;
        [1-2][0-9]) ;;
        3[0-1]) ;;
        *) return 1 ;;
        esac

... by combining pattern alternatives with "|".

Code:
        case $3 in
        0[1-9] | [1-2][0-9] | 3[0-1]) ;;
        *) return 1 ;;
        esac

Note that I did not double-quote $3. There's nothing wrong with doing so, but it's not necessary. Double quotes only protect against field splitting and pathname expansion (file globbing), neither of which is performed on the word which follows "case".

Regards,
Alister

Last edited by alister; 03-25-2013 at 11:35 AM..
Sponsored Links
    #7  
Old 03-25-2013
amazigh42 amazigh42 is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 23 January 2014, 3:59 AM EST
Posts: 77
Thanks: 11
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by alister View Post
You omitted the ")" after the pattern.

On an unrelated note, in case you are not aware, the following can be re-written ...

... by combining pattern alternatives with "|".

Code:
        case $3 in
        0[1-9] | [1-2][0-9] | 3[0-1]) ;;
        *) return 1 ;;
        esac

Note that I did not double-quote $3. There's nothing wrong with doing so, but it's not necessary. Double quotes only protect against field splitting and pathname expansion (file globbing), neither of which is performed on the word which follows "case".

Regards,
Alister
Thank you for yours advices.
But look with this magenta pattern. it doesn't work.
Have you an idea why ?



Code:
#!/bin/bash
#########################################################################
valid() {
case "$1" in
        [E,F,Q,P][A,C,S][A,I,V,P][D,R,T,V]) ;;
        *) return 1 ;;
        esac

        case "$2" in
        [PU][IPS][AGS][1-8]) ;;
        ISO[9-10]ORA) ;;
        *) return 1 ;;
        esac

        case "$3" in
        [0-1][0-9]) ;;
        2[0-3]) ;;
        *) return 1 ;;
        esac

        case "$4" in
        [0-5][0-9]) ;;
        *) return 1 ;;
        esac

        case "$5" in
        server) ;;
        exploitation) ;;
        *) return 1 ;;
        esac

        OLDIFS="$IFS"
        IFS="-"
               set -- $1 # $1=YYYY, $2=MM, $3=DD
        IFS="$OLDIFS"

        case "$1" in
        [2-9][0-9][0-9][0-9]) ;;
        *) return 1 ;;
        esac

        case "$2" in
        0[1-9]) ;;
        1[0-2]) ;;
        *) return 1 ;;
        esac

        case "$3" in
        0[1-9] | [1-2][0-9] | 3[0-1]) ;;
        *) return 1 ;;
        esac

        return 0
}
process() {
echo "$1 $2 $3 $4 $5 $6"
}

        if ! valid $1 $2 $3 $4 $5 $6 # Ignore invalid lines
        then
                echo "$1 $2 $3 $4 $5 $6 is invalid"
        else
        process $1 $2 $3 $4 $5 $6
fi

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[DATE] Pattern matching notation amazigh42 Shell Programming and Scripting 4 03-11-2013 03:02 AM
Convert decimal notation to ANSI point code notation aavam Shell Programming and Scripting 4 06-28-2011 09:16 PM
sed - matching pattern one but not pattern two RobertBerrie Shell Programming and Scripting 2 05-07-2009 09:04 PM
counting the lines matching a pattern, in between two pattern, and generate a tab d.chauliac Shell Programming and Scripting 4 03-19-2009 01:30 PM
comment/delete a particular pattern starting from second line of the matching pattern imas Shell Programming and Scripting 4 10-13-2008 02:37 AM



All times are GMT -4. The time now is 06:17 PM.