grep lines separated with semicolon

Hello,

I would like to kindly ask you for help. I have a file with some lines in one row separated by semicolon. I need to find out, if the line I have in different variable is included in this file. e.g

I have a file foo.txt with lines

A=hello there;hello world;hello there world

In the my shell script I grep the lines separated with semicolon like:

CONST_A=`cat foo.txt | grep "A=" | cut -d "=" -f1`

CONST_A now contains:

hello there;hello world;hello there world

Now I have different variable, which contains string "hello there" and I want to find if the constant contains exactly this string. What would be the best way to do that? Is it grep or awk? And how? Thank you very much for your answer.

how about this one

echo "A=hello there;hello world;hello there world" | sed '/hello there/s/^[a-zA-Z]*=//g'

Just use the shell test function:

CONST_A=$(sed -n '/A=/s/^.*=//p' foo.txt)
VAL_A="hellow there"

if [ "$CONST_A" = "$VAL_A" ]
then
   echo "Exact match"
fi
 
if [[ "$CONST_A" = *"$VAL_A"* ]]
then
    echo "$VAL_A is in the string"
fi

Take for example

KEY=hello world;foobar;another

You search for "foo". You would want to find "foobar" and return success? Probably not. So you want to find [icode](^|;)foo(;|$)[icode] only.
Depending on shell... you could do the entire of it in bash if it is small configuration or some such:

$ IFS='=' read -r key val <<< "$str"
$ echo "[[$key]] [[$val]]"
[[KEY]] [[hello world;foobar;another]]
$ IFS=';' read -ra vals <<< "$val"
$ declare -p vals
declare -a vals='([0]="hello world" [1]="foobar" [2]="another")'
$ in_array() { find=$1; shift; for v; do [[ $v = $find ]] && return 0; done; return 1; }
$ in_array foo "${vals[@]}"; echo $?
1
$ in_array foobar "${vals[@]}"; echo $?
0

You could also dummy ";" on front and back
then search for ";value;" eg:

$ VALS="hello world;foobar;another"
$ [[ ";$VALS;" = *";hello world;"* ]] && echo yes
yes

Try with AWK:

awk -F";" '
/^A=/ {
  for ( i=1; i<=NF; ++i ) {
    if (index($i, "hello world")>0 ) {
      print $i
    }
  }
}' foo.txt

Thank you very much for your answers. On the end I used dummy ; and it works good