Issue with files comparison, help me with a logic


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue with files comparison, help me with a logic
# 1  
Old 06-05-2013
Issue with files comparison, help me with a logic

Can someone please help me with a unix logic for below. I tried to get the desired output by using change capture condition in Datastage but its not working properly. i have two files file1, file2 as below.
file1
HTML Code:
ROW_NO  VEND_NO CODE AIR_D OCEAN_D
----------------------------------------
1  5000315 CPT 01-JAN-00 20-JUN-09 
2  5000315 DUR 01-JAN-00 23-JUN-09
3  5000004 DUR 24-JUN-09 24-JUN-30
4  5000004 CPT 19-JUN-09 24-JUN-09
5  5000202 RTM 31-DEC-98 21-MAY-01
6  5000175 KEE 01-JAN-00 01-MAY-97
file2
HTML Code:
VEND_NO CODE AIR_D OCEAN_D
----------------------------------
5000004 DUR 24-JUN-09 24-JUN-30
5000315 DUR 01-JAN-00 23-JUN-20 
5000202 RTM 31-DEC-13 21-MAY-15 
5000175 KEE 01-JAN-00 01-MAY-97 
5000004 CPT 19-JUN-00 24-JUN-14
5000203 MBA 30-DEC-98 16-JUN-00 
5000315 CPT 01-JAN-10 20-JUN-20
For a combination of VEND_NO, CODE values in every row in file1, have to compare AIR_D, OCEAN_D values in file1 with the values in file2 for the same combination of VEND_NO, CODE values. if there is a mismatch, need to replace with the values found in file2. For example, for the combination of VEND_NO=5000315, CODE=CPT in file1, when comparing AIR_D,OCEAN_D values in file1 with file2, there is a mismatch. so these two values should be replaced. output should be as below.
HTML Code:
ROW_NO  VEND_NO CODE AIR_D OCEAN_D
----------------------------------------
1  5000315 CPT 01-JAN-10 20-JUN-20 
2  5000315 DUR 01-JAN-00 23-JUN-20
3  5000004 DUR 24-JUN-09 24-JUN-30
4  5000004 CPT 19-JUN-00 24-JUN-14
5  5000202 RTM 31-DEC-13 21-MAY-15
6  5000175 KEE 01-JAN-00 01-MAY-97
# 2  
Old 06-05-2013
Could this help you ?
Code:
awk 'NR==FNR{a[$2"|"$3]=$1;next}
a[$1"|"$2]{print a[$1"|"$2],$0}' file1 file2  | sort -nk1

This User Gave Thanks to pravin27 For This Post:
# 3  
Old 06-06-2013
Hi Pravin, Thanks a lor for the reply.

If you dont mind , can you pelase explain the logic as i have to add one more column to compare both files ?
# 4  
Old 06-06-2013
Hi JSKOBS,

I tried my best to explain you..., hope this will help you to understand..

NR = This is the number of input records awk has processed since the beginning of the program's execution
FNR = is the current record number in the current file. FNR is incremented each time a new record is read.
It is reinitialized to zero each time a new input file is started.
For a single file, FNR is nothing but NR (line number)
And for more than one file, NR and FNR will be equal for the first processed file,
but on the first line of the second and subsequent files FNR will start from 1 again.
Condition NR==FNR is true for first file only, so statement a[$2"|"$3]=$1;next will be executed for file1.
a[$2"|"$3]=$1 means array "a" with index field #2 and field #3 concatenated with symbol "|" and we assign value of field#1
In your case our array will be

a[5000315|CPT]=1
a[5000315|DUR]=2
a[5000004|DUR]=3
and so on....
next will skips over the rest of the body.
Second line is to process secod file as condition NR==FNR will failed while processing second file.
a[$1"|"$2] = this is if condition checking if field #1 and field #2 from file2 i.e. a[5000004|DUR] is true or false.
While processing first file we have filled the array , so it is true then print the value of a[5000004|DUR] which is 3 and then $0 i.e. complete line of file2.
After that sending this o/p to sort command , to sort numeric and on field1
This User Gave Thanks to pravin27 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - 2 files comparison without for loop - multi-line issue

Greetings Experts, I need to handle the views created over monthly retention tables for which every new table in YYYYMMDD format, there is equivalent view created and the older table which might be dropped, the view over it has to be re-created over a dummy table so that it doesn't fail.... (2 Replies)
Discussion started by: chill3chee
2 Replies

2. Shell Programming and Scripting

Stupid issue with number comparison

Hello all, I'm having an infuriating issue with number comparison. Basically I've written a script that runs in cygwin that SSH's to 4 servers, figures out a success percentage and if it is less than a certain point, triggers an alarm. I've managed to get it to connect to the servers, figure out... (5 Replies)
Discussion started by: DeCoTwc
5 Replies

3. Shell Programming and Scripting

Issue with String Comparison (if)

Hi, I was trying to do a string comparison using if. However, the comparison result is getting treated as a executable statement. I'm not sure where I'm making the mistake! $ typeset TEST_VAR='YUP' $ if ; then echo 'Got It!'; fi; ksh: : not found. Any help is appreciated! (3 Replies)
Discussion started by: waterdrop
3 Replies

4. UNIX for Advanced & Expert Users

Strange Number comparison issue

Hi, I am comparing two numbers, but it gives strange results: My Code: if then echo "True" else echo "False" fi This code gives False for the follwoing comparison where as True for the following: Any reason for this? Both Should have given False... I am using... (9 Replies)
Discussion started by: shihabvk
9 Replies

5. Programming

random number logic -- issue

I use standard C random number generation logic in my application. long nCounter; long lRndNo; char rand; srand48(nCounter); lRndNo = lrand48(); sprintf(rand,"%010.10d",lRndNo); However we always find that the rand generated starts with '0','1' or '2'. I could not logically prove if... (1 Reply)
Discussion started by: asutoshch
1 Replies
Login or Register to Ask a Question