If test grep.... always returns 0 status

Hi all. I am trying to compare and filter two files. I have a bigfile.txt of names and ids and a smallfile.txt of ids only. What I am trying to do is use a while read loop to read the ids in the bigfile and then echo the name and id only if the id exists in the small file. Basically, I'm trying to find the names that correspond to the ids in the small file.

Here's what I've got so far:

big=bigfile.txt
small=smallfile.txt

while read user id
do
    if test 'grep -q $id $small'; then
        echo "ID exists"
    else
        echo "ID does not exist"
    fi
done < $big

My problem now is that the first condition is always evaluated as true. No matter whether the id exists or not in the smallfile, the output is always the same - "ID Exists" for every line of input in bigfile.

I've manually confirmed that the exit code should be non-0 for multiple ids in bigfile. Any ideas on what I'm doing wrong?

nawk 'FNR==NR {small[$1];next} {print $1, ($1 in small)?"exists":"does not exist"}' small big

Actually what you are trying to do ?

what is your exact need.?

try the below

big=bigfile.txt
small=smallfile.txt

while read user id
do
     grep -q $id $small ; 
     if [ $? -eq 0 ]
     then
        echo "ID exists"
     else
        echo "ID does not exist"
    fi
done < $big

That would be because your grep command isn't actually executed, but is a plain string, which is always true. Lose the test command and the quotes, as test is only needed when you're doing a comparison, but not when the command returns a true or false exit code.

From looking at your grep attempt, you have assumed that the id cannot occur as part of a name. The following makes the same assumption, but it's more efficient than looping and repeatedly reading a file. The output should be lines in bigfile.txt whose id is present in smallfile.txt:

grep -Ff smallfile.txt bigfile.txt

Regards,
Alister