If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > OK, ZONK and space oddysey

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-27-04, 00:20
node node is offline
Registered User
 
Join Date: Mar 2004
Posts: 2
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 00:24.
Reply With Quote
  #2 (permalink)  
Old 03-27-04, 05:48
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
Reply With Quote
  #3 (permalink)  
Old 03-27-04, 08:38
node node is offline
Registered User
 
Join Date: Mar 2004
Posts: 2
Great!!!
This is it what I was was looking for!!!
Thank you!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On