The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
awk - comparing files dbrundrett Shell Programming and Scripting 6 01-18-2009 10:51 PM
Comparing two files superstar003 Forum Support Area for Unregistered Users & Account Problems 1 05-08-2008 03:34 AM
Comparing two files.. padarthy Shell Programming and Scripting 1 08-29-2007 08:01 AM
Comparing two files... paqman Shell Programming and Scripting 12 08-08-2007 03:45 AM
comparing shadow files with real files terrym UNIX for Advanced & Expert Users 4 02-09-2007 02:38 AM

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

Join Date: Oct 2004
Posts: 87
Comparing files

I have a file called X, which contains the following:

10
100
200
300


I then have file Y, which containts the following:

10
200
500
800

I want to write a script that will compare the contents of Y with the contents of X and ONLY return values in Y that does not exist in X (output to file Z).

Could someone please point me in the right direction?

Thanks
  #2 (permalink)  
Old 04-25-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
Code:
awk ' FILENAME=="X" {arrx[$0]++}
       FILENAME=="Y" {arry[$0]++}         
     END {for (i in arry) 
         {if(arrx[i]==0) {print i}  } }'  X Y > newfile
Sample run with code in t.awk:
Quote:
csadev:/home/jmcnama> cat X Y
1
2
3
4
5
6
8
9
10
1
2
3
4
5
6
7
8
9
10
csadev:/home/jmcnama> t.awk
csadev:/home/jmcnama> cat newfile
7
  #3 (permalink)  
Old 04-25-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
If your fgrep supports the -f option, you can do

Code:
fgrep -vxf X Y
There are various more tedious ways to express this, e.g. using a sed script which you'd construct from the contents of X. Search the forums for "fgrep -f" "sed" or someething like that to see the numerous earlier threads on similar topics.
  #4 (permalink)  
Old 04-25-2008
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
look into 'man comm'
  #5 (permalink)  
Old 04-25-2008
soliberus soliberus is offline
Registered User
  
 

Join Date: Oct 2004
Posts: 87
thank you all for your replies

in this instance fgrep -vxf works the best. thank you era
  #6 (permalink)  
Old 04-29-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
Does not this one address your issue?

Code:
comm -13 file1 file2
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 05:42 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