Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2004
    Posts
    2

    Unanswered: OK, ZONK and space oddysey

    Code:
    #!/bin/sh
    
    STRING="hal,kohal,obahalta,hal9000"
    if [ `echo $STRING | grep hal` ]; then
      echo OK
    else
      echo ZONK
    fi
    STRING has inside "hal"
    even if i change STRING to "kohal,obahalta,hal9000"
    i always get OK
    how to recognize if there is in STRING exactly 'hal' separated by comas?
    Last edited by node; 03-27-04 at 01:24.

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Try the following Regular Expression with egrep (or grep -E) : ^(.*,)?hal(,.*)$

    This RE recognizes the following strings (_ is any char):
    ___,hal,___
    hal,___
    ___,hal
    hal
    Code:
    if echo "$STRING" | egrep -q '^(.*,)?hal(,.*)?$' ; then
      echo OK
    else
      echo ZONK
    fi
    With ksh you can use the = operator instead of grep, with the pattern : ?(*,)hal?(,*)
    Code:
    #!/usr/bin/ksh
    if [[ "$STRING" = ?(*,)hal?(,*) ]] ; then
      echo OK
    else
      echo ZONK
    fi
    If you really want to recognize only the first form (___,hal,___), modify your RE by ',hal,'
    Code:
    #!/bin/sh
    
    STRING="hal,kohal,obahalta,hal9000"
    if [ `echo $STRING | grep ',hal,` ]; then
      echo OK
    else
      echo ZONK
    fi
    Jean-Pierre.

  3. #3
    Join Date
    Mar 2004
    Posts
    2
    Great!!!
    This is it what I was was looking for!!!
    Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •