Compare fields in 2 files using AWK

Hi unix gurus,

I have a urgent requirement, I need to write a AWK script to compare each fields in 2 files using AWK.

Basically my output should be like this.

file1

row|num1|num2|num3
1|one|two|three
2|one|two|three

file2

row|num1|num2|num3
1|one|two|three
2|one|two|four

output should be

row|num1|num2|num3
1|y|y|y|y
2|y|y|n

This is kind of urgent, can you please help me , I am new to AWk scripting.

Thanks in Advance
Rashmi

awk -F\| 'NR==FNR{a[NR]=$0;next}FNR==1{print;next}{split(a[FNR],b,"|");for (i=1;i<=NF;i++){if (i==1){printf $1};if (FNR!=1 && i!=1 && b==$i){printf FS "y"};if (FNR!=1 && i!=1 && b!=$i){printf FS "n"};};printf "\n"}' file 1 file2
awk -F'|' '{getline x<f; split(x,F,"|")} NR>1{for(i=2;i<=NF;i++)$i=(F==$i)?"y":"n"}1' OFS="|" f=file2 file1
1 Like

Thank you guys ...just wanted to know if this works in SUN OS unix.

Hi.

It should, if you use nawk or /usr/xpg4/bin/awk. That's to say not the awk that's probably in your path (/usr/bin/awk).

I have a urgent requirement with AWK and I am new to AWK scripting , kindly help me with the below problem

I would be having file where I would get information about the input files and the fields to compare .

info_file to AWK
------------
input_1=file1
input_2=file2
key_field|display|compare
Y|seq|y
N|Num1|n
N|Num2|y

file1
-----
seq|num1|num2
-------------
000|Abc|bcd
001|asd|qwe
002|efg|hij

file2
-------

seq|num1|num2
-------------
000|abc|bcd
001|asd|qwe
002|efg|iop

From the info_file, I will get the file1 and file2 names,

1) if key_filed is marked to Y, then the file 2 value should be specified,
2) display vaules should be the header of the output file
3) under compare if its marked to Y, then I need to compare those files if its marked to N, then those fields should be ignored .
4) And if the values match then I need to mark it to Y , else it should be marked N.
5) If all the fileds match in a record, then that should not appear in the output file.

so based on the above requirement, my output would be something like below

output
--------

seq|num1|num2
-----------
000||y
002||N

And I am working on sun OS.

Thanks
rashmi

---------- Post updated at 09:50 PM ---------- Previous update was at 12:12 PM ----------

Please anybody ...please help me ...it is kind of urgent...