diff 2 files > file3, but records in various order


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting diff 2 files > file3, but records in various order
# 8  
Old 04-14-2010
I can't see a simple solution without sorting the data. This approach has only one sort. It works.

Code:
cat sourcefile1 sourcefile2 | sort | uniq -u > file3


Btw and off topic. I'm sure that the uuoc advocates can find some complicated way of concatonating the files.
# 9  
Old 04-14-2010
Quote:
Originally Posted by methyl
Btw and off topic. I'm sure that the uuoc advocates can find some complicated way of concatonating the files.
Certainly not a UUOC advocate, but isn't that the same as:

Code:
sort -u sourcefile1 sourcefile2 > file3

# 10  
Old 04-14-2010
Nope. sort -u prints one instance of duped lines. uniq -u does not print those lines (which in this case are unwanted).
# 11  
Old 04-14-2010
Power

Quote:
Originally Posted by alister
Nope. sort -u prints one instance of duped lines. uniq -u does not print those lines (which in this case are unwanted).
I missed the -u uniq option in methyl's post. Oops, sorry Smilie

(at least the cat isn't neccessary Smilie)
# 12  
Old 04-14-2010
Wonderful, thanks! It looks like both of these work Smilie
Code:
grep -vxf file1 file2 > file3
grep -vxf file2 file1 >> file3

using a single grep wouldn't see changes to the other file. The second version looks more elegant:
Code:
cat file1 file2 | sort | uniq -u > file3

though I'm not sure which method would use less cpu/mem? I'm sorting a few million records with this so resource usage may be an issue. Actually I have to repeat this process for many text files in a directory, so I may want to automate piping the ls of the dir and to this process, so at the end I get a dump of only unique records from all the files.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Want to grep records in alphabetical order from a file and split into other files

Hi All, I have one file containing thousands of table names in single column. Now I want that file split into multiple files e.g one file containing table names starting from A, other containing all tables starting from B...and so on..till Z. I tried below but it did not work. for i in... (6 Replies)
Discussion started by: shekhar_4_u
6 Replies

2. Shell Programming and Scripting

Diff 3 files, but diff only their 2nd column

Guys i have 3 files, but i want to compare and diff only the 2nd column path=`/home/whois/doms` for i in `cat domain.tx` do whois $i| sed -n '/Registry Registrant ID:/,/Registrant Email:/p' > $path/$i.registrant whois $i| sed -n '/Registry Admin ID:/,/Admin Email:/p' > $path/$i.admin... (10 Replies)
Discussion started by: kenshinhimura
10 Replies

3. Shell Programming and Scripting

Delete records in reverse order

Hi all, i have dynamic file 'xyz.txt', records always look likes below format ... 0000021 RET 31-MAR-1984 FAP 0000021 DTA 14-JAN-2003 CNV 0000021 DTA 25-MAR-2012 DTA 0000021 DTA 26-MAR-2012 DTA ################################################# 0000021 DTA ... (4 Replies)
Discussion started by: krupasindhu18
4 Replies

4. Shell Programming and Scripting

Compare two files with different number of records and output only the Extra records from file1

Hi Freinds , I have 2 files . File 1 |nag|HYd|1|Che |esw|Gun|2|hyd |pra|bhe|3|hyd |omu|hei|4|bnsj |uer|oeri|5|uery File 2 |nag|HYd|1|Che |esw|Gun|2|hyd |uer|oi|3|uery output : (9 Replies)
Discussion started by: i150371485
9 Replies

5. Shell Programming and Scripting

sort the files based on timestamp and execute sorted files in order

Hi I have a requirement like below I need to sort the files based on the timestamp in the file name and run them in sorted order and then archive all the files which are one day old to temp directory My files looks like this PGABOLTXML1D_201108121235.xml... (1 Reply)
Discussion started by: saidutta123
1 Replies

6. Shell Programming and Scripting

PERL Scripting: Diff 2 files and save output to file3

Hi, I need to create a script to compare 2 files and store the output in a 3rd file. This is how I do manually, but since I need to do this for about 150 files every week, I am trying to automate it using perl. diff -u file1 file2 > file3.patch For my script, - I have 2 files... (4 Replies)
Discussion started by: script2010
4 Replies

7. UNIX for Dummies Questions & Answers

cat file1 file2 > file3

file1 has pgap500 500 file2 has bunch of data cat file1 file2 > file3 cp file2 file3.dat then vi pgap500 500 onto 1st line compare file3 and fil3.dat, they are not the same. any idea ? the 1st line, i want to put pg500 xxx ---------- Post updated at 07:35 AM ---------- Previous... (2 Replies)
Discussion started by: tjmannonline
2 Replies

8. Shell Programming and Scripting

How to arange records in a particular order?

Hi guys, I have a problem please help if you have any solutions. I have two files. FILE1 having records separated by '>' FILE1 >LOG_Ps04g30040.1|12004.m08110|test lc-like prot, test1 MGASPSREEAHSNSSFSGNGKAMAVASSASSSGSNQAQSKRAPALHMFQEIVAEKDFTAS LPKQ* >ab|22329085|xyz|PP_194957.2|... (4 Replies)
Discussion started by: sam_2921
4 Replies

9. Shell Programming and Scripting

Find duplicates from multuple files with 2 diff types of files

I need to compare 2 diff type of files and find out the duplicate after comparing each types of files: Type 1 file name is like: file1.abc (the extension abc could any 3 characters but I can narrow it down or hardcode for 10/15 combinations). The other file is file1.bcd01abc (the extension... (2 Replies)
Discussion started by: ricky007
2 Replies

10. Shell Programming and Scripting

diff 2 files; output diff's to 3rd file

Hello, I want to compare two files. All records in file 2 that are not in file 1 should be output to file 3. For example: file 1 123 1234 123456 file 2 123 2345 23456 file 3 should have 2345 23456 I have looked at diff, bdiff, cmp, comm, diff3 without any luck! (2 Replies)
Discussion started by: blt123
2 Replies
Login or Register to Ask a Question