Compare fields in 2 files using AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare fields in 2 files using AWK
# 1  
Old 10-20-2010
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
Code:
row|num1|num2|num3
1|one|two|three
2|one|two|three

file2

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

output should be

Code:
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

Last edited by radoulov; 10-20-2010 at 06:28 AM.. Reason: Code tags, please!
# 2  
Old 10-20-2010
Code:
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]==$i){printf FS "y"};if (FNR!=1 && i!=1 && b[i]!=$i){printf FS "n"};};printf "\n"}' file 1 file2

# 3  
Old 10-20-2010
Code:
awk -F'|' '{getline x<f; split(x,F,"|")} NR>1{for(i=2;i<=NF;i++)$i=(F[i]==$i)?"y":"n"}1' OFS="|" f=file2 file1

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 10-20-2010
Thank you guys ...just wanted to know if this works in SUN OS unix.
# 5  
Old 10-20-2010
Quote:
Originally Posted by rashmisb
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).
# 6  
Old 10-21-2010
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...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is there a UNIX command that can compare fields of files with differing number of fields?

Hi, Below are the sample files. x.txt is from an Excel file that is a list of users from Windows and y.txt is a list of database account. $ head -500 x.txt y.txt ==> x.txt <== TEST01 APP_USER_PROFILE USER03 APP_USER_PROFILE TEST02 APP_USER_EXP_PROFILE TEST04 APP_USER_PROFILE USER01 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

Compare 2 files and find missing fields awk

Hello experts! I have 2 files. file1 is a list file containing uniquely names. e.g.: name1 number number name2 number number name5 number number name10 number number ... file2 is a data file arbitrary containing the names of file1 in paragraphs separated by "10" e.g. name4 ... (3 Replies)
Discussion started by: phaethon
3 Replies

3. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

4. UNIX for Dummies Questions & Answers

Compare 2 fields in 2 files

I am trying to compare two files (separted by a pipe) using 2 fields (field 1,3 from fileA and 1,2 from fileB) if the two files match i want the whole record of fileA adding the extra fields left from fileB. 1. A.txt cat|floffy|12|anything|anythings cat|kitty|15|lala|lalala... (6 Replies)
Discussion started by: sabercats
6 Replies

5. Shell Programming and Scripting

Compare fields in files

Hi, I need the most efficient way of comparing the following and arriving at the result I have a file which has entries like, File1: 1|2|5|7|8|2|3|6|3|1 File2: 1|2|3|1|2|7|9|2 I need to compare the entries in these two file with those of a general file, 1|2|3|5|2|5|6|9|3|1... (7 Replies)
Discussion started by: pradebban
7 Replies

6. Shell Programming and Scripting

compare fields in different files

HI I'm having some troubles to compare and permut diffrent fields indexed with another filed like the following example `: file1 1 1 2 2 3 3 file2 7 1 9 2 10 3 result------------------- (6 Replies)
Discussion started by: yassinegoth
6 Replies

7. Shell Programming and Scripting

AWK Compare files, different fields, output

Hi All, Looking for a quick AWK script to output some differences between two files. FILE1 device1 1.1.1.1 PINGS device1 2.2.2.2 PINGS FILE2 2862 SITE1 device1-prod 1.1.1.1 icmp - 0 ... (4 Replies)
Discussion started by: stacky69
4 Replies

8. Shell Programming and Scripting

Compare two files using awk or sed, add values in a column if their previous fields are same

Hi All, I have two files file1: abc,def,ghi,5,jkl,mno pqr,stu,ghi,10,vwx,xyz cba,ust,ihg,4,cdu,oqw file2: ravi,def,kishore ramu,ust,krishna joseph,stu,mike I need two output files as follows In my above example, each row in file1 has 6 fields and each row in file2 has 3... (3 Replies)
Discussion started by: yerruhari
3 Replies

9. UNIX for Dummies Questions & Answers

Compare two files using awk or sed, add values in a column if their previous fields are same

Hi All, I have two files file1: abc,def,ghi,5,jkl,mno pqr,stu,ghi,10,vwx,xyz cba,ust,ihg,4,cdu,oqw file2: ravi,def,kishore ramu,ust,krishna joseph,stu,mike I need two output files as follows In my above example, each row in file1 has 6 fields and each row in file2 has 3... (1 Reply)
Discussion started by: yerruhari
1 Replies

10. UNIX for Advanced & Expert Users

Compare two files using awk or sed, add values in a column if their previous fields are same

Hi All, I have two files file1: abc,def,ghi,5,jkl,mno pqr,stu,ghi,10,vwx,xyz cba,ust,ihg,4,cdu,oqw file2: ravi,def,kishore ramu,ust,krishna joseph,stu,mike I need two output files as follows In my above example, each row in file1 has 6 fields and each row in file2 has 3... (1 Reply)
Discussion started by: yerruhari
1 Replies
Login or Register to Ask a Question