How to find previous string based on an input string?

Hi,

I did some research but cannot find the right solution so hopefully someone can help me here.

I have a long string format like:

VAR=111:aaaa,222:bbb,333:ccc

it could be

VAR=111:aaa,222:bbb,333:ccc,444:ddd, etc

what I looking for is eg.

if I give ccc, it will return me 333

if aaa, return 111.

a string before :$INPUT

Hi, try (using bash or ksh93) :

while IFS=: read val key
do
  if [ "$key" = "$INPUT" ]; then
    echo "$val"
  fi
done <<< "${VAR//,/$'\n'}"
$ INPUT=aaa
$ while IFS=: read val key; do   if [ "$key" = "$INPUT" ]; then     echo "$val";   fi; done <<< "${VAR//,/$'\n'}"
111
$ INPUT=bbb
$ while IFS=: read val key; do   if [ "$key" = "$INPUT" ]; then     echo "$val";   fi; done <<< "${VAR//,/$'\n'}"
222

that works perfectly!
thank you