Find Unmatched name from given lists.. | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Find Unmatched name from given lists..

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-28-2012
Killer420 Killer420 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 30 December 2012, 3:47 AM EST
Posts: 4
Thanks: 4
Thanked 0 Times in 0 Posts
Code 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.
Sponsored Links
    #2  
Old 12-28-2012
Subbeh Subbeh is offline
Registered User
 
Join Date: May 2011
Last Activity: 30 July 2014, 9:23 AM EDT
Posts: 328
Thanks: 35
Thanked 86 Times in 85 Posts

Code:
comm -13 list1 list2

The Following User Says Thank You to Subbeh For This Useful Post:
Killer420 (12-28-2012)
Sponsored Links
    #3  
Old 12-28-2012
Killer420 Killer420 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 30 December 2012, 3:47 AM EST
Posts: 4
Thanks: 4
Thanked 0 Times in 0 Posts
i want solution in shell script using loop.
    #4  
Old 12-28-2012
Subbeh Subbeh is offline
Registered User
 
Join Date: May 2011
Last Activity: 30 July 2014, 9:23 AM EDT
Posts: 328
Thanks: 35
Thanked 86 Times in 85 Posts
Why do you need a loop for this?

You can loop over the output of the comm command:

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

Or use something like this:

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


Last edited by Subbeh; 12-28-2012 at 09:41 AM..
The Following User Says Thank You to Subbeh For This Useful Post:
Killer420 (12-28-2012)
Sponsored Links
    #5  
Old 12-28-2012
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 1 August 2014, 8:21 PM EDT
Location: Galactic Empire
Posts: 3,358
Thanks: 230
Thanked 1,192 Times in 1,124 Posts
Assuming 2 files list1 and list2 has list in same line:

Code:
# 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:

Code:
#!/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:

Code:
# ./search.sh
lmn xyz

The Following User Says Thank You to Yoda For This Useful Post:
Killer420 (12-28-2012)
Sponsored Links
    #6  
Old 12-28-2012
Killer420 Killer420 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 30 December 2012, 3:47 AM EST
Posts: 4
Thanks: 4
Thanked 0 Times in 0 Posts
spcl thnx to bipinajith....
exact ans. which i want..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find the difference between two server lists proactiveaditya Shell Programming and Scripting 3 11-01-2012 09:47 AM
Shell Script to Create non-duplicate lists from two lists mlv_99 Shell Programming and Scripting 7 04-06-2010 08:59 PM
`for' unmatched rechever Shell Programming and Scripting 2 06-16-2009 03:01 PM
done' unexpected and do' unmatched LRoberts Shell Programming and Scripting 6 02-02-2009 10:49 AM
else unmatched b.hamilton Shell Programming and Scripting 7 10-10-2007 05:03 AM



All times are GMT -4. The time now is 02:30 AM.