String comparison problem

Hi,

can someone please help me!!! urgent!

I have a strange issue here. I grep for 2 strings from a txt files and compare the string value. Though the string values are the same, they are compared as different values. Please help

Case-1
--------
Here I grep for 2 different field values and compare them. Though these 2 values are the same in the file, they are compared to be different values and I see the o/p as "Not Equal"

-----
tmp1=`grep Expected samFile.txt | head -1 | cut -d "," -f2 | sed 's/ //g'`
tmp2=`grep Expected samFile.txt | head -1 | cut -d "," -f4 | sed 's/ //g'`
 
if [[ "$tmp1" = "$tmp2" ]]
then
        echo "Equal!!! "
else
        echo "Not Equal!!! "
fi
 

Case-1
--------
Here I manually store these values directly to the variables. In this case, the comparison results is as expected, "Equal"

-----
tmp3=SAMPLE123
tmp4=SAMPLE123
 
if [[ "$tmp3" = "$tmp4" ]]
then
        echo "Equal!!! "
else
        echo "Not Equal!!! "
fi

Why didn't the strings were not equal in Case-1??

Maybe you should remove the spaces before you do the cut?

Can you show the raw input from samFile.txt?

edit: In hind-sight the cut before sed thing wasn't so bright :slight_smile: Show the input file!

Well from what we know of samFile.txt (nothing...) the comparison may certainly fail for you are looking at -f2 in tmp1 and -f4 in tmp2, whereas tmp3 and tmp4 are identical...

(ksh)

# cat tst
a1111,222 2222,333333,22222 22,555555 555
b1111,2222222,333333,444444 4444444,555555 555
# set -- $(grep a tst | sed '1s/ //g;s/,/ /g;q')
# [[ "$2" == "$4" ]] && echo "Equal" || echo "Not Equal"
Equal
# set -- $(grep b tst | sed '1s/ //g;s/,/ /g;q')
# [[ "$2" == "$4" ]] && echo "Equal" || echo "Not Equal"
Not Equal