File Compare and Create New File with Diff


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users File Compare and Create New File with Diff
# 1  
Old 12-14-2006
File Compare and Create New File with Diff

I need to compare File A with File B and create FILE C with the difference record only. What I mean is File A has 3 records and File B has 4 records, so FILE C will only have 1 record (becuase that record is in FILE B and not in File A). Hope I am making sense. The data layout is that each data element is within double quotes and each field is seperated by a comma. In the example below after the second name there is a double quote and 3 commas, the first comma is the field seperator and the other 2 commas are for fields that there is no data, hence the commas.

File A

"00000000155","JOHN","SMITH",,,"1944-06-03",
"00000001166","SMITH","JOHN",,,"1947-24-01",
""00000002189","BOB","SMITH",,,"1947-03-04",

File B

"00000000155","JOHN","SMITH",,,"1944-06-03",
"00000001166","SMITH","JOHN",,,"1947-24-01",
""00000002189","BOB","SMITH",,,"1947-03-04",
""00000002263","SMITH","JOHN",,,"1948-02-24",


I am not sure what uniz command to use, I haev done some reading and it looks like I can use the diff command or maybe even teh awk command. If someone can provide assistance here , I would be greatly appreciate it.

Sincerely
# 2  
Old 12-14-2006
Hm, try using the comm command. Its does exactly what you want to do.
# 3  
Old 12-14-2006
File Compare and Create New File with Diff

Thanks for your resposne, but I tried the comm command and it is not working, this si what I tried

comm -23 file1 file2

Any assistance would be greatly apprecaited.

Thanks
# 4  
Old 12-14-2006
Both files need to be sorted first for comm to work.
# 5  
Old 01-06-2007
File Compare in Unix

I had posted this earlier about 3 weeks ago and had recieved a response and I did sort both the files and the comm command is still not working.

Can someone please assist me, I would really appreciate it.

Below is what I am trying to do
I need to compare File A with File B and create FILE C with the difference record only. What I mean is File A has records and File B has records, so FILE C will only have the records which are not in File A. Hope I am making sense. The data layout is that each data element is within double quotes and each field is seperated by a comma. In the example below after the second name there is a double quote and 3 commas, the first comma is the field seperator and the other 2 commas are for fields that there is no data, hence the commas

Below are the sample file layout

File A


"00004006676","JOHN","SMITH",,,"1944-06-03",
"00004200004","JOHN","SMITH",,,"1944-06-03",
"00004200075","JOHN","SMITH",,,"1944-06-03",
"00004200101","JOHN","SMITH",,,"1944-06-03",
"00004200155","JOHN","SMITH",,,"1944-06-03",
"00004200245","JOHN","SMITH",,,"1944-06-03",
"00004200318","JOHN","SMITH",,,"1944-06-03",
"00004201166","JOHN","SMITH",,,"1944-06-03",
"00004202042","JOHN","SMITH",,,"1944-06-03",
"00004202189","JOHN","SMITH",,,"1944-06-03",
"00004202234","JOHN","SMITH",,,"1944-06-03",
"00004202263","JOHN","SMITH",,,"1944-06-03",
"00004202298","JOHN","SMITH",,,"1944-06-03",
"00004202365","JOHN","SMITH",,,"1944-06-03",
"00004202373","JOHN","SMITH",,,"1944-06-03",
"00004202491","JOHN","SMITH",,,"1944-06-03",
"00004202640","JOHN","SMITH",,,"1944-06-03",
"00004202745","JOHN","SMITH",,,"1944-06-03",
"00004202994","JOHN","SMITH",,,"1944-06-03",
"00004203032","JOHN","SMITH",,,"1944-06-03",
"00004203044","JOHN","SMITH",,,"1944-06-03",
"00004203238","JOHN","SMITH",,,"1944-06-03",
"00004203539","JOHN","SMITH",,,"1944-06-03",
"00004203553","JOHN","SMITH",,,"1944-06-03",
"00004203655","JOHN","SMITH",,,"1944-06-03",
"00004203669","JOHN","SMITH",,,"1944-06-03",
"00004203722","JOHN","SMITH",,,"1944-06-03",
"00004203762","JOHN","SMITH",,,"1944-06-03",
"00004203860","JOHN","SMITH",,,"1944-06-03",
"00004203887","JOHN","SMITH",,,"1944-06-03",
"00004207046","JOHN","SMITH",,,"1944-06-03",

File B

"00004006676","JOHN","SMITH",,,"1944-06-03",
"00004200004","JOHN","SMITH",,,"1944-06-03",
"00004200075","JOHN","SMITH",,,"1944-06-03",
"00004200101","JOHN","SMITH",,,"1944-06-03",
"00004200125","JOHN","SMITH",,,"1944-06-03",
"00004200130","JOHN","SMITH",,,"1944-06-03",
"00004200155","JOHN","SMITH",,,"1944-06-03",
"00004200245","JOHN","SMITH",,,"1944-06-03",
"00004200318","JOHN","SMITH",,,"1944-06-03",
"00004201166","JOHN","SMITH",,,"1944-06-03",
"00004202042","JOHN","SMITH",,,"1944-06-03",
"00004202189","JOHN","SMITH",,,"1944-06-03",
"00004202234","JOHN","SMITH",,,"1944-06-03",
"00004202263","JOHN","SMITH",,,"1944-06-03",
"00004202298","JOHN","SMITH",,,"1944-06-03",
"00004202307","JOHN","SMITH",,,"1944-06-03",
"00004202365","JOHN","SMITH",,,"1944-06-03",
"00004202373","JOHN","SMITH",,,"1944-06-03",
"00004202491","JOHN","SMITH",,,"1944-06-03",
"00004202640","JOHN","SMITH",,,"1944-06-03",
"00004202745","JOHN","SMITH",,,"1944-06-03",
"00004202994","JOHN","SMITH",,,"1944-06-03",
"00004203032","JOHN","SMITH",,,"1944-06-03",
"00004203238","JOHN","SMITH",,,"1944-06-03",
"00004203539","JOHN","SMITH",,,"1944-06-03",
"00004203553","JOHN","SMITH",,,"1944-06-03",
"00004203655","JOHN","SMITH",,,"1944-06-03",
"00004203669","JOHN","SMITH",,,"1944-06-03",
"00004203722","JOHN","SMITH",,,"1944-06-03",
"00004203762","JOHN","SMITH",,,"1944-06-03",
"00004203860","JOHN","SMITH",,,"1944-06-03",
"00004203887","JOHN","SMITH",,,"1944-06-03",
"00004207046","JOHN","SMITH",,,"1944-06-03",

The final results in File C would be the following records since these records are in File B and not in File A

"00004200125","JOHN","SMITH",,,"1944-06-03",
"00004200130","JOHN","SMITH",,,"1944-06-03",
"00004202307","JOHN","SMITH",,,"1944-06-03",

Sincerely

Hadi Lalani

Last edited by vino; 01-06-2007 at 03:47 PM.. Reason: remove email address...
# 6  
Old 01-06-2007
guiguy, I just closed a duplicate thread and I see that vino had to edit your post to remove your email address. Please read the rules:

(4) Do not 'bump up' questions if they are not answered promptly. No duplicate or cross-posting and do not report a post where your goal is to get an answer more quickly.

(10) Don't post your email address and ask for an email reply. The forums are for the benefit of all, so all Q&A should take place in the forums.

As for your question, I copied your data into files which I named file_A and file_B. Then I tried:
Code:
$ comm -13 file_A file_B
"00004200125","JOHN","SMITH",,,"1944-06-03",
"00004200130","JOHN","SMITH",,,"1944-06-03",
"00004202307","JOHN","SMITH",,,"1944-06-03",
$
$

The man page is clear that comm -13 is what you need. You were suppressing the wrong column. You tried:
comm -23
which was wrong. There are only two other ways to print a single column:
comm -13
comm -12

Why not try them before you give up?
# 7  
Old 02-28-2007
how for files not sorted

Quote:
Originally Posted by Perderabo
guiguy, I just closed a duplicate thread and I see that vino had to edit your post to remove your email address. Please read the rules:

(4) Do not 'bump up' questions if they are not answered promptly. No duplicate or cross-posting and do not report a post where your goal is to get an answer more quickly.

(10) Don't post your email address and ask for an email reply. The forums are for the benefit of all, so all Q&A should take place in the forums.

As for your question, I copied your data into files which I named file_A and file_B. Then I tried:
Code:
$ comm -13 file_A file_B
"00004200125","JOHN","SMITH",,,"1944-06-03",
"00004200130","JOHN","SMITH",,,"1944-06-03",
"00004202307","JOHN","SMITH",,,"1944-06-03",
$
$

The man page is clear that comm -13 is what you need. You were suppressing the wrong column. You tried:
comm -23
which was wrong. There are only two other ways to print a single column:
comm -13
comm -12

Why not try them before you give up?

How to get the common columns in two files that are not sorted...?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

2. Shell Programming and Scripting

Trying to use diff output to compare to a separate file

I have two files: smw:/working/iso_testing # cat a QConvergeConsoleCLI-1.1.03-49.x86_64.rpm aaa_base-13.2+git20140911.61c1681-1.3.i586.rpm acpica-20140724-2.1.2.i586.rpm test.rpm smw:/working/iso_testing # cat b QConvergeConsoleCLI-1.1.03-49.x86_64.rpm... (12 Replies)
Discussion started by: jedlund21
12 Replies

3. Shell Programming and Scripting

How to compare 2 files and create a result file with unmatched lines from first file.?

HI, I have 2 text files. file1 and file2. file1.txt (There are no duplicates in this file) 1234 3232 4343 3435 6564 6767 1213 file2.txt 1234,wq,wewe,qwqw 1234,as,dfdf,dfdf 4343,asas,sdds,dsds 6767,asas,fdfd,fdffd I need to search each number in file1.txt in file2.txt's 1st... (6 Replies)
Discussion started by: Little
6 Replies

4. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

5. Shell Programming and Scripting

Help with create multiple directories under diff file systems

Hi, Need help ...I want to create multiple directories in different /file systems using for loop..eg.../ORCL_data01/oradata/orcl/ctl. ../ORCL_data01/oradata/orcl/data. ../ORCL_data01/oradata/orcl/redo. Script :- ========= for dir in `ls -d... (8 Replies)
Discussion started by: Linux6.5
8 Replies

6. Shell Programming and Scripting

Compare two CSV files and put the difference in third file with line no,field no and diff value.

I am having two csv files i need to compare these files and the output file should have the information of the differences at the field level. For Example, File 1: A,B,C,D,E,F 1,2,3,4,5,6 File 2: A,C,B,D,E,F 1,2,4,5,5,6 out put file: (12 Replies)
Discussion started by: karingulanagara
12 Replies

7. Shell Programming and Scripting

Compare file timestamp with current date. Diff must be 1 hour.

Hello, I've created the script below to compare the content of two files with a delay of an hour. After an hour, the lines that exist in both files, will be printed and executed. The script now uses a counter to countdown 50 minutes. But what I would prefer is to check the file timestamp of... (3 Replies)
Discussion started by: taipan
3 Replies

8. Shell Programming and Scripting

Compare two csv files by two colums and create third file combining data from them.

I've got two large csv text table files with different number of columns each. I have to compare them based on first two columns and create resulting file that would in case of matched first two columns include all values from first one and all values (except first two colums) from second one. I... (5 Replies)
Discussion started by: agb2008
5 Replies

9. Shell Programming and Scripting

Compare two files and output diff to third file

I have made several attempts to read two files of ip addresses and eliminate records from file1 that are in file2. My latest attempt follows. Everything works except my file3 is exactly the same as file1 and it should not be. # !/usr/bin/bash # # NoInterfaces # Utility will create a file... (8 Replies)
Discussion started by: altamaha
8 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