Find Unmatched name from given lists..

i have two lists,
list1 => abc jones oracle smith ssm tty
list2 => abc jones lmn smith ssm xyz

now i want to print only those names which are present in list2 and want to remove names from list2 which presents in list1.

so i want OUTPUT => lmn xyz
because "abc jones smith ssm" from list2 are present in list1.

I want to do it in shell script using loop... plz help.

comm -13 list1 list2
1 Like

i want solution in shell script using loop.

Why do you need a loop for this?

You can loop over the output of the comm command:

comm -13 list1 list2 | while read l ; do echo $l ; done

Or use something like this:

while read list2 ; do
        grep $list2 list1 &>/dev/null
        if [ $? -eq 1 ] ; then
                echo $list2
        fi
done < list2
1 Like

Assuming 2 files list1 and list2 has list in same line:

# cat list1
abc jones oracle smith ssm tty

# cat list2
abc jones lmn smith ssm xyz

Here is a solution in BASH using only built-ins:

#!/bin/bash

FLAG=0
while read list2_line
do
        for list2_word in $list2_line
        do
                while read list1_line
                do
                        for list1_word in $list1_line
                        do
                                [[ "$list2_word" == "$list1_word" ]] && FLAG=1
                        done
                        [[ $FLAG -eq 0 ]] && echo -e "$list2_word \c"; FLAG=0
                done < list1
        done
done < list2
echo

Here is the output:

# ./search.sh
lmn xyz
1 Like

spcl thnx to bipinajith....
exact ans. which i want..