To get unique numbers from two files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers To get unique numbers from two files
# 1  
Old 06-08-2008
To get unique numbers from two files

here i have two files:
file 1
1
2
3
4
5
5
6
7
8
9

file 2
4
5
6
6
8
8
9

How to get file 3 containingSmilie no repetitions )
1
2
3
4
5
6
7
8
9

i tried
sort -o file1.srt file1
sort -o file2.srt file2
comm -13 file1.srt file2.srt > new
cat file1.srt new > file3
file3 should not contain repetitions.

Thanks.
# 2  
Old 06-08-2008
found the solution

This worked....Let me know if there is a better way to do it.

sort -o file1.dat.srt file1.dat
sort -o file2.dat.srt file2.dat
comm -13 file1.dat.srt file2.dat.srt > file.dat.srt
cat file1.dat.srt file.dat.srt > file.srt

for i in `cat file.srt | uniq`
do
echo $i >> file3.dat
done
# 3  
Old 06-08-2008
With sort or uniq:

Code:
sort -u file1 file2>file3

Code:
<file1 uniq file2>file3

Or Awk (use nawk or /usr/xpg4/bin/awk on Solaris):

Code:
awk >file3 '!x[$1]++' file1 file2

Or use perl:

Code:
perl >file3 -nle'print unless $_{$_}++' file1 file2


Last edited by radoulov; 06-08-2008 at 06:27 PM..
# 4  
Old 06-08-2008
thanks

Awesome!!!


awk command did not work on command line, but when i put that in script and executed, it worked : any reason ?

perl worked well.



thanks.
# 5  
Old 06-08-2008
Quote:
Originally Posted by i.scientist
Awesome!!!

Both worked but not sorted..but thats fine.

awk command did not work on command line, but when i put that in script and executed, it worked : any reason ?
What was the error?

Quote:
perl worked well.

can i sort them too ? in asingle command ?
With perl:

Code:
perl >file3 -nle'$_{$_}++;END{print join "\n", sort keys %_}' file1 file2

But you don't need perl and awk when sort or uniq are sufficient
(see my previous post).

P.S. If you use uniq you should sort the input first.

Last edited by radoulov; 06-08-2008 at 07:01 PM..
# 6  
Old 06-08-2008
This is the error :
$ awk '!x[$1]++' file1.dat file2.dat > file.dat
x[: Event not found.

sort works only if the file has numbers with same digits:
I mean it will sort like this
1
2
21
23
3
4
45
And not in exact manner like 1 2 3 4 21 23 45
But my problem is solved as all numbers have same no. of digits.


Anyways, I just want to let you know.
But I got my answer. I will use either perl or awk or sort.
THANK YOU

Last edited by i.scientist; 06-08-2008 at 07:09 PM.. Reason: Previous post contains the answer
# 7  
Old 06-08-2008
Quote:
Originally Posted by i.scientist
This is the error :
$ awk '!x[$1]++' file1.dat file2.dat > file.dat
x[: Event not found.
[...]
I suppose you're using (t)csh.
You shouldn't Smilie
Anyway, this may work:

Code:
awk '\!x[$1]++' file1 file2>file3

Quote:
sort works only if the file has numbers with same digits:
I mean it will sort like this
1
2
21
23
3
4
45
And not in exact manner like 1 2 3 4 21 23 45
Just tell sort to thread them as numbers
(use the -n option):

Code:
$ sort <<<'20
3
1'
1
20
3

$ sort -n <<<'20
3
1'
1
3
20



Quote:
And this command. Did not work or I may not have used properly.
This is how I used :
$ <file1.dat uniq file2.dat > file3
$ more file1.dat
2 8 7 1 3 6 4 9 5 5 4 3 2 1 2 4 2
$ more file2.dat
1 2 4 3 5 6 7 8 12 11 13 14 45 56 23 0 1
$ more file3
1 2 4 3 5 6 7 8 12 11 13 14 45 56 23 0 1
[...]
Yes, I corrected my previous post: to get the output you want from uniq, the input should be sorted.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing unique numbers from each file

I have some files named file1, file2, fille3......etc. These files are in a folder f1. The content of files are shown below. I would like to count the unique pairs of third column in each file. some files have no data. It should be printed as zero. Your help would be appreciated. file1 ARG... (1 Reply)
Discussion started by: samra
1 Replies

2. Shell Programming and Scripting

Unique entries based on a range of numbers.

Hi, I have a matrix like this: Algorithm predicted_gene start_point end_point A x 65 85 B x 70 80 C x 75 85 D x 10 20 B y 125 130 C y 120 140 D y 200 210 Here there are four tab-separated columns. The first column is the used algorithm for prediction, and there are 4 of them A-D.... (8 Replies)
Discussion started by: flyfisherman
8 Replies

3. Shell Programming and Scripting

Extract unique files

In a incoming folder i have list of files like below,i want to pick the unique files to process the job. if same file contain more than one then it should pick latest date modified file to process. drwxrwsrwx 2 n308799 infagrp 256 May 20 17:42 Final_Working drwxrwsrwx 2... (1 Reply)
Discussion started by: katakamvivek
1 Replies

4. UNIX for Dummies Questions & Answers

getting unique lines from 2 files

hi i have used comm -13 <(sort 1.txt) <(sort 2.txt) option to get the unique lines that are present in file 2 but not in file 1. but some how i am getting the entire file 2. i would expect few but not all uncommon lines fro my dat. is there anything wrong with the way i used the command? my... (1 Reply)
Discussion started by: anurupa777
1 Replies

5. Shell Programming and Scripting

unique random numbers awk

Hi, I have a small piece of awk code (see below) that generates random numbers. gawk -F"," 'BEGIN { srand(); for (i = 1; i <= 30; i++) printf("%s AM329_%04d\n",$0,int(36 * rand())+1) }' OFS=, AM329_hole_names.csv The code works fine and generates alphanumeric numbers like AM329_0001,... (2 Replies)
Discussion started by: theflamingmoe
2 Replies

6. Shell Programming and Scripting

Replace string ids with unique numbers

Hello, I have a file with a 1000 ids in the form of strings. I want to replace each id with a unique numbers in the whole file. each id is repeating in all the columns. I know I can use sed command but there are many ids in file which are need to be converted example of input file B752... (4 Replies)
Discussion started by: ryan9011
4 Replies

7. Shell Programming and Scripting

Unique files in a given directory

I keep all my files on a NAS device and copy files from it to usb or local storage when needed. The bad part about this is that I often have the same file on numerous places. I'd like to write a script to check if the files in a given directory exist in another. An example: say I have a... (7 Replies)
Discussion started by: cue
7 Replies

8. Shell Programming and Scripting

How to generate 10.000 unique numbers?

hello, does anybody can give me a hint on how to generate a lot of numbers which are not identically via scripting etc? (7 Replies)
Discussion started by: xrays
7 Replies

9. UNIX for Dummies Questions & Answers

Getting unique list of numbers using grep

Hi, I am going to fetch a list of numbers that starts with "0032" from a file with a format like the given below: " 0032459999 0032458888 0032457777 0032451111 0032452222 0032453333 0032459999 0032458888 0032457777 0032451111 0032452222 0032453333 " I want to get a unique... (6 Replies)
Discussion started by: tinku
6 Replies

10. Shell Programming and Scripting

Shell script to check the unique numbers in huge data

Friends, I have to write a shell script,the description is---- i Have to check the uniqueness of the numbers in a file. A file is containing 200thousand tickets and a ticket have 15 numbers in asecending order.And there is a strip that is having 6 tickets that means 90 numbers.I... (7 Replies)
Discussion started by: namishtiwari
7 Replies
Login or Register to Ask a Question