Compare 2 sorted files | 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.

Compare 2 sorted files

Shell Programming and Scripting


Tags
unix commands

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-21-2008
varungupta varungupta is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 15 July 2010, 5:53 AM EDT
Location: Pune, Dehradun (INDIA), Michigan(US)
Posts: 206
Thanks: 0
Thanked 1 Time in 1 Post
Lightbulb Compare 2 sorted files

Hi all,

please give me the commands using which i can compare 2 sorted files and get the difference in third file, indiating where the difference is from either file1 or file2.

as:

File1 (Original file)

GARRY
JOHN
JULIE
SAM

---------------
File2

DEV
GARRY
JOHN
JOHNIEE
JULIE
RAMEE
SAM
SAMYEE
-------------------
OUTPUT in file3 should have :

DEV
JOHNIEE
RAMEE
SAMYEE


Using awk and diff/cmp commands.
Sponsored Links
    #2  
Old 01-21-2008
dennis.jacob's Avatar
dennis.jacob dennis.jacob is offline Forum Advisor  
Registered User
 
Join Date: Feb 2007
Last Activity: 30 March 2014, 9:33 AM EDT
Location: Singapore/Cochin
Posts: 871
Thanks: 0
Thanked 12 Times in 11 Posts
Try this:

Code:
comm -13 file1 file2


Code:
diff file1 file2 | awk '/^>/{print $2 }'

Sponsored Links
    #3  
Old 01-21-2008
varungupta varungupta is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 15 July 2010, 5:53 AM EDT
Location: Pune, Dehradun (INDIA), Michigan(US)
Posts: 206
Thanks: 0
Thanked 1 Time in 1 Post
Error

Quote:
Originally Posted by dennis.jacob View Post
Try this:

Code:
comm -13 file1 file2


Code:
diff file1 file2 | awk '/^>/{print $2 }'


Could you please tell me why -13 is being used in comm command ?
    #4  
Old 01-22-2008
ranjithpr ranjithpr is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 20 August 2010, 3:34 AM EDT
Posts: 175
Thanks: 0
Thanked 3 Times in 3 Posts
Try this one

grep -x -v -f F1 F2
Sponsored Links
    #5  
Old 01-22-2008
varungupta varungupta is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 15 July 2010, 5:53 AM EDT
Location: Pune, Dehradun (INDIA), Michigan(US)
Posts: 206
Thanks: 0
Thanked 1 Time in 1 Post
MySQL

Quote:
Originally Posted by ranjithpr View Post
grep -x -v -f F1 F2
Hey,

thanks man, hope this'll work fine.



Varun.
Sponsored Links
    #6  
Old 01-23-2008
dennis.jacob's Avatar
dennis.jacob dennis.jacob is offline Forum Advisor  
Registered User
 
Join Date: Feb 2007
Last Activity: 30 March 2014, 9:33 AM EDT
Location: Singapore/Cochin
Posts: 871
Thanks: 0
Thanked 12 Times in 11 Posts
Quote:
Originally Posted by varungupta View Post
Could you please tell me why -13 is being used in comm command ?


-1 suppress lines unique to left file

-2 suppress lines unique to right file

-3 suppress lines that appear in both files
Sponsored Links
    #7  
Old 01-25-2008
varungupta varungupta is offline
Registered User
 
Join Date: Feb 2007
Last Activity: 15 July 2010, 5:53 AM EDT
Location: Pune, Dehradun (INDIA), Michigan(US)
Posts: 206
Thanks: 0
Thanked 1 Time in 1 Post
Lightbulb

Quote:
Originally Posted by ranjithpr View Post
grep -x -v -f F1 F2
Hey,

I have attached 2 files in this mail, please find those, in which i am doing comparisions.
Contents of EAIDVBR1.txt are standard and Contents of EAIDQBR1.txt are mixed.
I want list of all strings from EAIDQBR1.txt that are not there in EAIDVBR1.

Please have a look and let me know. As i have tries "grep -x -v -f F1 F2" command on these files and its not working effectively.

Let me know asap.
Thanks
Varun
Attached Files
File Type: txt EAIDQBR1.txt (6.1 KB, 17 views)
File Type: txt EAIDVBR1.txt (4.6 KB, 14 views)
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
sort the files based on timestamp and execute sorted files in order saidutta123 Shell Programming and Scripting 1 09-18-2011 02:47 PM
combine multiple files by column into one files already sorted! ahmedamro Shell Programming and Scripting 4 02-26-2011 03:53 AM
write a program in c in unix that display the files(includ sub-direc and files within) in a sorted pappu kumar jha UNIX for Dummies Questions & Answers 1 01-26-2011 01:06 PM
merging two files with sorted integers in c brett01 Programming 4 12-21-2009 02:32 PM
concatenate files sorted by date docaia Shell Programming and Scripting 2 08-16-2008 02:23 AM



All times are GMT -4. The time now is 10:06 PM.