Comma Seperated List of Values

Hi,

I have a comma seperated list of values:

export list="red,blue,white,yellow"

Given a value in a variable "look", i want to check whether the value is available in the above list. But the result should be based on exact string match and not part of the string.

I am using following command:
echo $list | grep $look

For Eg:

  1. When look="red", the above command returns "red,blue,white,yellow". This is CORRECT as i consider the value is present.

  2. When look="green", the above command returns "" (BLANK). This is CORRECT as i consider the value is not present.

  3. When look="low", the above command returns "red,blue,white,yellow". This is WRONG as low is not word but part of the 'yellow' word. The result should ideally give me ""(BLANK).

Can anybody help me out to overcome my problem OR is there any better way to check whether a given exact word is present in a list.

TIA

Warm Regards,
Ramesh

See if the approach in String extraction from user input - sh helps.

try using

echo $list|grep -w $look

But in HP-UX, -w option is not available for grep command

but I am using HP-UX as well, and I have -w option for grep

Following is error i am getting when the command echo $list|grep -w $look is executed:

grep: illegal option -- w
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] -e pattern_list...
[-f pattern_file...] [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] [-e pattern_list...]
-f pattern_file... [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] pattern [file...]

Instead of -w, try this

grep "\<$look\>"

its working fine for me,
try using egrep

echo $list|grep -E "(^|,)$look(,|$)"

Thanks, Ygor.... this echo $list|grep -E "(^|,)$look(,|$)" solved the problem.

and Thanks a lot to everyone.....