LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-06-2012, 03:10 AM   #1
ravi_nandula
Member
 
Registered: Sep 2011
Posts: 81

Rep: Reputation: Disabled
Comparing 2 files


Hi everyone,

I have same problem with the comparing 2 files .
My requirement is ...

cat 1234
1
2
3
4
5
6
67
7
............
cat file1
1
3
5
67
...........................
Now I want "file1" to compare with "1234"...
1 is present in both the file so it should be printed
but 7 is not there in the "file1" so it should not be printed.

so can anyone help me to do this.

Thanks in Advance
 
Old 07-06-2012, 03:18 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
If you want common lines printed, look at the "comm" command. The two files need to be sorted.
comm -12 <(sort file1) <(sort file2)

The command normally prints out 3 columns; unique to the first file, unique to the second, common to both. The -12 options suppresses the first 2 columns of output leaving just the lines common to both.
 
Old 07-06-2012, 03:22 AM   #3
jsaravana87
Member
 
Registered: Aug 2011
Location: Chennai,India
Distribution: Redhat,Centos,Ubuntu,Dedian
Posts: 558
Blog Entries: 5

Rep: Reputation: Disabled
You can compare the file using sdiff

#sdiff 1324 file1
 
Old 07-06-2012, 03:23 AM   #4
ravi_nandula
Member
 
Registered: Sep 2011
Posts: 81

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jschiwal View Post
If you want common lines printed, look at the "comm" command. The two files need to be sorted.
comm -12 <(sort file1) <(sort file2)

The command normally prints out 3 columns; unique to the first file, unique to the second, common to both. The -12 options suppresses the first 2 columns of output leaving just the lines common to both.
thanks for immediate reply.
actually I have one file with 500 host names and other file with list of 200 host names.
I want to find whether the 2nd set of 200 host names are present in the 1st file of 500 host name....

when I trying to use the command which u get gave....it's showing me the error msg " file 2 not in sorted order"......."file 1 not in sorted order"
 
Old 07-06-2012, 03:46 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
If you are interested in items in list 2 not in list 1, you want items unique to list 2, so suppress columns 1 & 3; comm -13.

Show the command you entered because my example used the sort command.

If you want new items in list 2 added to list 1, then create a temporary file catting the unique list with file1, then mv the temp file to file1.

P.S.
try adding the "-u" option to the sort commands.
comm -12 <(sort -u file1) <(sort -u file2)

Last edited by jschiwal; 07-06-2012 at 03:50 AM.
 
Old 07-06-2012, 03:54 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
How about:
Code:
grep -f 200_file 500_file | wc -l
 
1 members found this post helpful.
Old 07-09-2012, 11:31 AM   #7
montel
Member
 
Registered: Jun 2012
Location: Canada
Distribution: Ubuntu/Debian/CentOS
Posts: 45

Rep: Reputation: 18
Try this:

Code:
i=0
while read line ; do
        output=`echo $line`
        if [[ -n "$output" ]]
        then
                myarray[$i]=$output
                i=`expr $i + 1`
        fi
done < "1234"

while read line ; do
        match=0
        dat=`echo "$line"`
        if [ -n "$dat" ]
        then
                for (( x=0 ; x < ${#myarray[@]} ; x++ )) do
                        if [ "${myarray[$x]}" == "$dat" ]
                        then
                                match=1
                                echo ${myarray[$x]}
                        fi
                done
        fi
        if [ $match = 0 ]
        then
             echo "no match"
        fi
done < "file1"
I believe that should accomplish what you want

Last edited by montel; 07-09-2012 at 02:25 PM. Reason: Forgot array counter
 
Old 07-09-2012, 01:39 PM   #8
Alchemikos
Member
 
Registered: Jun 2012
Location: Porto Alegre-Brazil
Distribution: Slackware- 14, Debian Wheezy, Ubuntu Studio, Tails
Posts: 88

Rep: Reputation: 6
Wink

Quote:
Originally Posted by ravi_nandula View Post
thanks for immediate reply.
actually I have one file with 500 host names and other file with list of 200 host names.
I want to find whether the 2nd set of 200 host names are present in the 1st file of 500 host name....

when I trying to use the command which u get gave....it's showing me the error msg " file 2 not in sorted order"......."file 1 not in sorted order"

Hello

I must sort first , as said before :

Code:
sort 1234 > fa ; sort file1 > fb ; comm -12  fa fb > out ; nano out
Cheers
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Comparing two files p3rcy Programming 10 02-14-2012 01:27 PM
Comparing two files ab52 Programming 10 12-01-2010 12:08 PM
comparing files newbiesforever Linux - Software 3 07-07-2010 04:20 PM
Using diff for comparing 2 files beep Programming 5 01-21-2005 01:51 PM
Comparing 2 Files xianzai Programming 2 05-23-2004 12:50 PM


All times are GMT -5. The time now is 02:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration