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
Can I know find syntax to find given date files bache_gowda Shell Programming and Scripting 3 03-26-2008 06:37 AM
Find duplicates from multuple files with 2 diff types of files ricky007 Shell Programming and Scripting 2 03-04-2008 01:46 PM
union of two files sherkaner Shell Programming and Scripting 3 10-02-2007 06:22 PM
Little bit weired : Find files in UNIX w/o using find or where command jatin.jain Shell Programming and Scripting 10 09-19-2007 07:47 AM
Find files older than 20 days & not use find halo98 Shell Programming and Scripting 2 05-18-2006 03:19 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-06-2005
stevefox stevefox is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 115
Question How to find union of two files

Is there a command in unix to find the union of two files and removing the union from one of the files?


e.g. I have two files input1.txt and input2.txt with the contents below:



Code:
$ more input1.txt
4
2
3
2

$ more input2.txt
5
4
4
8
2

I want to find the union of the two and remove the union from input1.txt to output the below:
3

Any help will be appreciated.
  #2 (permalink)  
Old 12-06-2005
futurelet futurelet is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 137

Code:
ruby -e 'puts IO.read($*[0]).to_a - IO.read($*[1]).to_a' dat1 dat2

  #3 (permalink)  
Old 12-06-2005
stevefox stevefox is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 115
Question

Thanks futurelet.
Is there a way to do this without using ruby and only using standard HP Unix commands?
  #4 (permalink)  
Old 12-06-2005
Livio Livio is offline
Registered User
  
 

Join Date: Oct 2005
Location: Brazil - Sao Paulo
Posts: 12
awk '{print $0}' input1.txt input2.txt |sort -u
  #5 (permalink)  
Old 12-06-2005
stevefox stevefox is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 115
Thanks Livio
However your code did not give input1.txt minus the union of input1.txt and input2.txt which is 3.

I worked out that I can do this by the code below but is there a simpler way to do this using standard Unix commands? (possibly in one line?)


Code:
cat input1.txt input2.txt | sort | uniq -d > union
cat input1.txt union | sort | uniq -c > union2
sed -n '/1 /p' union2 > union3
sed -e 's/   1 //g' union3


Last edited by stevefox; 12-06-2005 at 10:46 PM..
  #6 (permalink)  
Old 12-07-2005
stevefox stevefox is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 115
I found out that it can be done by


Code:
sort -u input1.txt > temp1.txt
sort -u input2.txt > temp2.txt
comm -23 temp1.txt temp2.txt

Closed Thread

Bookmarks

Tags
unix commands

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 11:45 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