Problem with pattern matching in .sh script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • forumsaregreat
    New Member
    • Aug 2007
    • 9

    Problem with pattern matching in .sh script

    Hello all,
    I am doing a pattern matching for a string in a shell script.
    Here an apostrophe ( ' ) is also a part of a string hence it should also be part of the pattern. But somehow i can't seem to be able to include it.
    I have tried using the backslash to escape it but to no success.
    Also tried putting ' inside two others like this ' ' ' .... but this doesn't work as well.
    Please help.

    My code look something like this :
    Code: ( text )

    if( $0 ~ /^"[-a-zA-ZÀ-ÖØ-öø-ÿ<> ;0-9]+"/ )
    {
    }
    else
    {
    }

    The string should should have ( " ) at the start and the end and in between alpha-numeric characters with some special chars.
    e.g : "Mont-l'pierre"

    Thanks !!
    Sid
  • rameshramesh
    New Member
    • Jul 2007
    • 7

    #2
    Try Using in groups which will say something like ( ) ( ) and instead of using [A-Za-Z] try using \w+

    Comment

    • rellaboyina
      New Member
      • Jan 2007
      • 55

      #3
      Originally posted by rameshramesh
      Try Using in groups which will say something like ( ) ( ) and instead of using [A-Za-Z] try using \w+

      You can use a backslash character(\) to escape the special meaning of the character ( ' ). It works.

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Originally posted by forumsaregreat
        Hello all,
        I am doing a pattern matching for a string in a shell script.
        Here an apostrophe ( ' ) is also a part of a string hence it should also be part of the pattern. But somehow i can't seem to be able to include it.
        I have tried using the backslash to escape it but to no success.
        Also tried putting ' inside two others like this ' ' ' .... but this doesn't work as well.
        Please help.

        My code look something like this :
        Code: ( text )

        if( $0 ~ /^"[-a-zA-ZÀ-ÖØ-öø-ÿ<> ;0-9]+"/ )
        {
        }
        else
        {
        }

        The string should should have ( " ) at the start and the end and in between alpha-numeric characters with some special chars.
        e.g : "Mont-l'pierre"

        Thanks !!
        Sid
        Sorry, but this forum is for questions related to Perl scripting, not Shell scripting. Unless of course you have a Perl question?


        Could a Moderator please move this posting to either the Linux/Unix/BSD forum or the Misc. Questions forum please? Thank you!

        Regards,

        Jeff

        Comment

        • prometheuzz
          Recognized Expert New Member
          • Apr 2007
          • 197

          #5
          Originally posted by numberwhun
          ...
          Could a Moderator please move this posting to either the Linux/Unix/BSD forum or the Misc. Questions forum please? Thank you!
          ...
          Done.

          ............... ........

          Comment

          • forumsaregreat
            New Member
            • Aug 2007
            • 9

            #6
            Firstly sorry for posting the question in the wrong section.

            And thanks everyone for your replies / suggestions.
            I was facing a strange problem in that the backslash did not escape the ( ' ). Nevertheless I used the hex value of ( ' ) and then it worked.
            Very strage to me and also makes the code shabby to read.

            Regards,

            Comment

            Working...