The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk - comparing files dbrundrett Shell Programming and Scripting 4 08-22-2008 07:23 AM
Comparing two files superstar003 Forum Support Area for Unregistered Users & Account Problems 1 05-08-2008 12:34 AM
Comparing two files.. padarthy Shell Programming and Scripting 1 08-29-2007 05:01 AM
Comparing two files... paqman Shell Programming and Scripting 12 08-08-2007 12:45 AM
comparing shadow files with real files terrym UNIX for Advanced & Expert Users 4 02-08-2007 11:38 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-22-2008
Registered User
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 138
Arrow Comparing two txt files - pls help

Hi,

I have some text files. I need a separate shell say 1.sh in which i can open a particular text file and compare with another txt file. For example:

1.log.txt contains

apple
ball
cat
goat

2.log.txt contains

goat
cat
lion
apple
fox

In my i.sh i need to write script to compare this two files and write the the o/p.The output should be written onto two different files such that one file contains the words that match and another contains the words that do not match.That is after i run 1.sh i should get common.txt and
notcommon.txt


common.txt should contains words present in both(common to both 1.og.txt & 2.log.txt ).i.e;

apple
ca
goat

nocommon.txt should contain the words present only in 2.log.txt and not in 1.log.txt . i.e;
lion
fox

Thanks in advance
JS
Reply With Quote
Forum Sponsor
  #2  
Old 01-22-2008
Registered User
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 138
I have searched the forum and got the answer for this...I used "comm"

Thanks
JS
Reply With Quote
  #3  
Old 01-22-2008
dj -------
 

Join Date: Feb 2007
Location: Cochin/Bangalore, India
Posts: 439
Remmeber that comm will work only for sorted files.
Reply With Quote
  #4  
Old 01-23-2008
Registered User
 

Join Date: Jun 2007
Location: Beijing China
Posts: 495
comm

Hi,

If you are not considering the sequence of the line in the original txt file. I think you can use comm command.

Try below one.

Code:
sort file1 > file1.temp
sort file2 > file2.temp
comm -12 file1.temp file2.temp > common.txt
comm -13 file1.temp file2.temp >uncommon.txt
Reply With Quote
  #5  
Old 01-23-2008
Registered User
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 138
That is a better code than waht i tried ..
i didnot sort my source files but it was almost in a sorted way .. But its always a good method to sort the files before compiling it ..
Thanks a lot for the quote ..
Regards
JS
Reply With Quote
  #6  
Old 01-23-2008
Registered User
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 138
Quote:
Originally Posted by dennis.jacob View Post
Remmeber that comm will work only for sorted files.
thanks for pointing it out .. Actually teh fils where sorted. Hence I ddint go for sorting .. The example i gave was for the name sake ..its not teh actual format of my file ..

thanks a lot for pointing it out .. because i didnt know about it .. its going to help me in the future ..

thanks a lot
JS
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:00 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0