How to get unique of file1 from file2 and save the output?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get unique of file1 from file2 and save the output?
# 1  
Old 06-13-2013
How to get unique of file1 from file2 and save the output?

Please help,

file1.txt
Code:
1
2
3
4
5

file2.txt
Code:
3
4
5
6
7

All I need is this result.txt
Code:
1
2

This code below is working with only with few records, but the problem is when I run with a large file like the the file is 2million records the result is theysame with file1
Code:
1
2
3
4
5

Code:
awk 'NR==FNR{a[$0];next}!($0 in a)' file1.txt file2.txt | xargs echo > result.txt

Question:
Why is it happen that it works in few records but not in more than 2million records?
Is it the command set it to timeout?
What could be the solution on this?

Thank you so much.

Last edited by Scott; 06-13-2013 at 06:48 PM.. Reason: Please use code tags
# 2  
Old 06-13-2013
Quote:
Originally Posted by richmac
All I need is this result.txt
1
2
If this is what you want, then you have to compare file2.txt with file1.txt

Also if you have only one field in each record, better use $1 instead of $0 to avoid blank spaces if any:
Code:
awk 'NR==FNR{a[$1];next}!($1 in a)' file2.txt file1.txt

Also note that awk does has a memory limitation, so it might throw an error when it exceeds the limit.
# 3  
Old 06-13-2013
simply use comm (be sure that both files are sorted, otherwise comm produces wrong output):

Code:
comm -23 file1.txt file2.txt


Last edited by Scott; 06-13-2013 at 06:47 PM.. Reason: Code tags
# 4  
Old 06-13-2013
Hi thanks for the reply but still theysame i try this command:

Code:
comm -2 -3 <(sort file1.txt) <(sort file2.txt) | xargs > result.txt

How to set limit timeout of this command? Or is their any way to solve my problem.

Thanks so much,

Last edited by Scott; 06-13-2013 at 06:48 PM.. Reason: Code tags
# 5  
Old 06-13-2013
did you try this?

Code:
sort file1.txt > file1
sort file2.txt > file2
comm -23 file1 file2 > result


Last edited by Scott; 06-13-2013 at 06:48 PM.. Reason: Code tags
# 6  
Old 06-13-2013
Hi doganaym,

Yes, but still theysame. Smilie

Thanks,

---------- Post updated at 06:48 PM ---------- Previous update was at 02:43 PM ----------

Anyone can help me on this?

Thank you so much,
Richie
# 7  
Old 06-14-2013
Did this help ?

Code:
sort file1 file2 | uniq -u

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

3. Shell Programming and Scripting

Compare columns of multiple files and print those unique string from File1 in an output file.

Hi, I have multiple files that each contain one column of strings: File1: 123abc 456def 789ghi File2: 123abc 456def 891jkl File3: 234mno 123abc 456def In total I have 25 of these type of file. (5 Replies)
Discussion started by: owwow14
5 Replies

4. Shell Programming and Scripting

If file1 and file2 exist then

HI, I would like a little help on writing a if statement. What i have so far is: #!/bin/bash FILE1=path/to/file1 FILE2=path/to/file2 echo ${FILE1} ${FILE2} if ] then echo file1 and file2 not found else echo FILE ok fi (6 Replies)
Discussion started by: techy1
6 Replies

5. Shell Programming and Scripting

[awk] split file1 and save it as var from file2

I have 2 files: file_1: file_2: expected result: name file: "artV1" "artV2" etc. I have: but why don;t work save to file 'out'?? (3 Replies)
Discussion started by: ffresz
3 Replies

6. Shell Programming and Scripting

look for line from FILE1 at FILE2

Hi guys! I'm trying to write something to find each line of file1 into file2, if line is found return YES, if not found return NO. The result can be written to a new file. Can you please help me out? FILE1 INPUT: WATER CAR SNAKE (in reality this file has about 600 lines each with a... (2 Replies)
Discussion started by: demmel
2 Replies

7. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

8. Shell Programming and Scripting

file1 newer then file2

Hello, I am new to shell scripting and i need to create a script with the following directions and I can not figure it out. Create a shell script called newest.bash that takes two filenames as input arguments ($1 and $2) and prints out the name of the newest file (i.e. the file with the... (1 Reply)
Discussion started by: mandylynn78
1 Replies

9. Shell Programming and Scripting

grep -f file1 file2

Wat does this command do? fileA is a subset of fileB..now, i need to find the lines in fileB that are not in fileA...i.e fileA - fileB. diff fileA fileB gives the ouput but the format looks no good.... I just need the contents alone not the line num etc. (7 Replies)
Discussion started by: vijay_0209
7 Replies

10. Shell Programming and Scripting

match value from file1 in file2

Hi, i've two files (file1, file2) i want to take value (in column1) and search in file2 if the they match print the value from file2. this is what i have so far. awk 'FILENAME=="file1"{ arr=$1 } FILENAME=="file2" {print $0} ' file1 file2 (2 Replies)
Discussion started by: myguess21
2 Replies
Login or Register to Ask a Question