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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
comparing PID values of 2 Files in shell Options marconi Shell Programming and Scripting 2 12-19-2007 01:02 PM
Need to find only unique values for a given tag across the files sudheshnaiyer UNIX for Dummies Questions & Answers 8 09-03-2007 01:53 AM
comparing within text files d3ck_tm AIX 3 02-28-2006 03:10 AM
Ignoring Some Text When Comparing 2 Files jimmyflip UNIX for Dummies Questions & Answers 1 09-21-2004 10:22 AM
comparing text files sjumma UNIX for Dummies Questions & Answers 2 11-12-2003 09:44 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-16-2008
smarty86 smarty86 is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 53
comparing 2 text files to get unique values??

Hi all,

I have got a problem while comparing 2 text files and the result should contains the unique values(Non repeatable).

For eg:

file1.txt
1
2
3
4

file2.txt
2
3

So after comaping the above 2 files I should get only 1 and 4 as the output. Pls help me out.
  #2 (permalink)  
Old 12-16-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,805

Code:
awk ' FILENAME=="file1" { if ($0 in arr) {continue} else {print $0}}
        FILENAME=="file2" {arr[$0]++ }
       ' file2 file1

  #3 (permalink)  
Old 12-16-2008
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
Quote:
Originally Posted by jim mcnamara View Post
Code:
awk ' FILENAME=="file1" { if ($0 in arr) {continue} else {print $0}}
        FILENAME=="file2" {arr[$0]++ }
       ' file2 file1
Jim,
if the content of file1 and file2 is reversed, this will not work.

here's another alternative (assuming a given record/line appears only once in a given file):

Code:
 nawk '{ if ($0 in a) delete a[$0]; else a[$0]} END { for (i in a) print i}' file1 file2


Last edited by vgersh99; 12-16-2008 at 02:27 PM..
  #4 (permalink)  
Old 12-16-2008
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
( cat file1.txt file2.txt ) | sort | uniq -c | awk '$1==1 {print $2}'


Last edited by vgersh99; 12-16-2008 at 01:51 PM..
  #5 (permalink)  
Old 12-16-2008
smarty86 smarty86 is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 53
@vgersh99 @Jim


Thanks Frens, ur code works awesome...

It gave me the proper o/p. hey if u don mind can u pls explain me the things in that?

anyways thanks for ur help...

Last edited by smarty86; 12-16-2008 at 02:02 PM..
  #6 (permalink)  
Old 12-16-2008
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
( cat file1.txt file2.txt ) | sort | uniq -c | nawk -v OFS='\t' '$1==1 {$1=$1;print}' | cut -f2-

  #7 (permalink)  
Old 12-16-2008
smarty86 smarty86 is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 53
@vgersh99

after ur changes the first code is working well man... thank u.. pls explain me about awk statement in that if u dont mind...
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




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


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0