Compare Records between to files and extract it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare Records between to files and extract it
# 1  
Old 06-29-2010
Compare Records between to files and extract it

I am not an expert in awk, SED, etc... but I really hope there is a way to do this, because I don't want to have to right a program. I am using C shell.

Code:
FILE 1              FILE 2

H0000000            H0000000
MA1                 MA1
CA1DDDDDD           CA1AAAAAA
MA2                 CD1DDDDDD
CA2ZZZZZZ           CA2YYYYYYY
MA3                 MA3
MA3KKKKKK           MA3KKKKKK
MA4                 MA5
MA4TTTTTT           MA5SSSSSS
T0000000            T0000000

As you can see I have 2 files. FILE 1 is the good file and FILE 2 is the bad file. Each File starts with a Header(H) record and ends with a trailer (T) record. Those records can just be written in and out, or ignored. Each M and C record go together. Each M record can have multiple C records, or no C records.

Basically what I am looking to do is to compare the C records from FILE 1 and FILE 2.

If the C record is on both files then I don't want to write it out to any file. If the C record is only on FILE 1 then I want to write it out to a GOOD FILE. If the record is only on FILE 2, then I want to write it out to a BAD FILE. I would like to try to keep the combination of M/C records together if possible. In the C records we are only comparing the Id codes (Column 4)(BBBBBB, KKKKKK, etc...)

The solution for the above example should be:

Code:
GOOD FILE           BAD FILE

H000000             H000000
MA2                 MA1
CA2ZZZZZZ           CA1AAAAAA
MA4                 MA2
MA4TTTTTT           CA2YYYYYY
T000000             MA5
                    CA5SSSSSS
                    T0000000

I would have to say that the file is not really in sorted order and not sure how to sort it.

Any help would be appreciated.
Thanks.

Moderator's Comments:
Mod Comment Use code tags!

Last edited by zaxxon; 06-30-2010 at 01:35 AM.. Reason: code tags
# 2  
Old 06-30-2010
Can I think column 3 is always MA number? And if it is 0, it must be head or tail. If it is true, the coding will be more simple.

For example, in file 2, line CA2YYYYYYY has no MA2, but in output bad file, you give extra MA2 line.
# 3  
Old 06-30-2010
My mistake. There should be a an MA2 in File 2.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare files to pull changed records only

Hi, I am using Sun Solaris - SunOS. I have two fixed width files shown below. I am trying to find the changes in the records in the Newfile.txt for the records where the key column matches. The first column is a key column (example: A123). If there are any new or deletion of records in the... (4 Replies)
Discussion started by: Saanvi1
4 Replies

2. Shell Programming and Scripting

Compare two files and extract

Assume we have two files - FileA and FileB. Content of files are as shown below : FileA:1001,value1,value4,value8,value9 1002,value4,value32,value46,value33 1503,value5,value45,value68,value53 1605,value4,value67,value56,value57 1073,value5,value45,value68,value53... (3 Replies)
Discussion started by: alnhk
3 Replies

3. Shell Programming and Scripting

awk - compare records of 1 file with 3 files

hi.. I want to compare records present in 1 file with those in 3 other files and print those records of file 1 which are not present in any of the files. for eg - file1 file2 file3 file4 1 1 5 7 2 2 6 9 3 4 5 6 7 8 9 ... (3 Replies)
Discussion started by: Abhiraj Singh
3 Replies

4. Shell Programming and Scripting

Compare multiple files, identify common records and combine unique values into one file

Good morning all, I have a problem that is one step beyond a standard awk compare. I would like to compare three files which have several thousand records against a fourth file. All of them have a value in each row that is identical, and one value in each of those rows which may be duplicated... (1 Reply)
Discussion started by: nashton
1 Replies

5. Shell Programming and Scripting

Compare multiple files, and extract items that are common to ALL files only

I have this code awk 'NR==FNR{a=$1;next} a' file1 file2 which does what I need it to do, but for only two files. I want to make it so that I can have multiple files (for example 30) and the code will return only the items that are in every single one of those files and ignore the ones... (7 Replies)
Discussion started by: castrojc
7 Replies

6. 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

7. Shell Programming and Scripting

Compare 2 files having different number of columns and records

Hi , My requirement is to Compare 2 files having different number of columns and records and get the ouptut containing all the non-matching records from File A(with all column values ) .Example data below : File A contains following : Aishvarya |1234... (4 Replies)
Discussion started by: aishvarya.singh
4 Replies

8. Shell Programming and Scripting

How to compare data from 2 zip files and capture the new records from file2 to a new file

I have 2 zip files which have about 20 million records in each file. file 2 will have additional records than file 1. I want to compare the records in both the files and capture the new records from file 2 into another file file3. Please help me with a command/script which provides me the desired... (8 Replies)
Discussion started by: koneru
8 Replies

9. Shell Programming and Scripting

Awk Compare Files w/Multiline Records

I'm trying to compare the first column values in two different files that use a numerical value as the key and output the more meaningful value found in the second column of file1 in front of the matching line(s) in file2. My problem is that file2 has multiple records. For example given: FILE1... (4 Replies)
Discussion started by: RacerX
4 Replies

10. UNIX for Dummies Questions & Answers

Best approach for a 10 min extract out of several log files with timestamped records

I have a task where I need to code a shell script to extract a 10 min range (10 min from now until now) extract of a log file. I taught I could simply use a command that would say something like Start=date - 10 min but I didn't find anything. Looks like the only solution would have to code a... (3 Replies)
Discussion started by: Browser_ice
3 Replies
Login or Register to Ask a Question