How to find union of two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find union of two files
# 1  
Old 12-06-2005
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  
Old 12-06-2005
Code:
ruby -e 'puts IO.read($*[0]).to_a - IO.read($*[1]).to_a' dat1 dat2

# 3  
Old 12-06-2005
Question

Thanks futurelet.
Is there a way to do this without using ruby and only using standard HP Unix commands?
# 4  
Old 12-06-2005
awk '{print $0}' input1.txt input2.txt |sort -u
# 5  
Old 12-06-2005
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  
Old 12-07-2005
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

# 7  
Old 12-07-2005
I think what your saying is output the contents of
file1 that are not in file2...

export LANG=C #for speed
sort input1.txt input2.txt input2.txt | uniq -u
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find command to find a word from list of files

I need to find a word '% Retail by State' in the folder /usr/sas/reports/RetailSalesTaxallocation. When I tried like below, -bash-4.1$ cd /usr/sas/reports/RetailSalesTaxallocation -bash-4.1$ find ./ -name % Retail by State find: paths must precede expression: Retail Usage: find ... (10 Replies)
Discussion started by: Ram Kumar_BE
10 Replies

2. Programming

Event driven programming / epoll / typedef union / session data array

Sorry for the “word salad” subject, but I wanted to cast a wide net for help. I've created an IP (Internet Protocol) server which serves HTTP, SMTP, and FTP requests. As you probably know, they all require creating a socket, listening on it, accepting connections, and then having a short... (3 Replies)
Discussion started by: John S.
3 Replies

3. Programming

GDB and GCC union

My concept may sound a bit cryptic but I what some startup information as to how we can use GDB APIs / debugging techniques in programs with GCC when we compile the program. We can definitely tell gcc to link GDB libs also. The ultimate aid would be that when the compiled programs executes it... (4 Replies)
Discussion started by: uunniixx
4 Replies

4. Web Development

Intersection and union of array by hash

Hi, A piece of script from Perl-cookbook I do not understand, and post here for explanation. The purpose is to find the element in either array (union), and in both array (intersection). Thank you in advance. @a=qw(1 3 5 6 7 8); @b=qw(2 3 5 7 9); foreach $e (@a, @b) {$union{$e}++ &&... (3 Replies)
Discussion started by: yifangt
3 Replies

5. Shell Programming and Scripting

awk: union regions

Hi all, I have difficulty to solve the followign problem. mydata: StartPoint EndPoint 22 55 2222 2230 33 66 44 58 222 240 11 25 22 60 33 45 The union of above... (2 Replies)
Discussion started by: phoeberunner
2 Replies

6. Shell Programming and Scripting

Text files union

How can i union two files that each of them contain text to one new file in unix shell scripting or in awk scripting language? (2 Replies)
Discussion started by: tal
2 Replies

7. Shell Programming and Scripting

union of two files

Given two files of the same format (For example number1|text1|number2) what is the command to print lines in file1 which do not occur in file2? diff command seems a bit complicated for me for this purpose. Please help!! Thank you very much. (3 Replies)
Discussion started by: sherkaner
3 Replies

8. Shell Programming and Scripting

Little bit weired : Find files in UNIX w/o using find or where command

Yes , I have to find a file in unix without using any find or where commands.Any pointers for the same would be very helpful as i am beginner in shell scritping and need a solution for the same. Thanks in advance. Regards Jatin Jain (10 Replies)
Discussion started by: jatin.jain
10 Replies

9. Shell Programming and Scripting

Find files older than 20 days & not use find

I need to find files that have the ending of .out and that are older than 20 days. However, I cannot use find as I do not want to search in the directories that are underneath the directory that I am searching in. How can this be done?? Find returns files that I do not want. (2 Replies)
Discussion started by: halo98
2 Replies
Login or Register to Ask a Question