Computing.Net > Forums > Programming > How to compare 2 files using awk?

How to compare 2 files using awk?

Reply to Message Icon

Original Message
Name: vinodjadye
Date: July 27, 2007 at 07:41:38 Pacific
Subject: How to compare 2 files using awk?
OS: Linux
CPU/Ram: Intel
Model/Manufacturer: Dell
Comment:

Hi,
I need to compare 2 files using awk, file1 & file2 line by line. Field 5 in everyline of file1 should be compared in whole file2 & if match is found then print that line in file2 & then print next 20 lines in file2. Continue the same process for all lines in file1. Can some one try this & send the script to me at the earliest.
Thx.


Vinod


Report Offensive Message For Removal


Response Number 1
Name: user57
Date: July 30, 2007 at 03:05:28 Pacific
Reply: (edit)

Try something like this...

awk -F: 'NR==FNR {a[$5]; next}

{
$5 in a
print $0
getline

for(i=1;i<=20;i++) {
print
getline
}
}' f1 f2


Report Offensive Follow Up For Removal

Response Number 2
Name: vinodjadye
Date: July 30, 2007 at 15:34:31 Pacific
Reply: (edit)

Can you provide your email ID?
I will provide you 2 files..you can test your code and then send that to me. I am novice on awk & Every time i run suggested code, i am not getting any o/p.

Vinod


Report Offensive Follow Up For Removal

Response Number 3
Name: vinodjadye
Date: July 30, 2007 at 16:12:58 Pacific
Reply: (edit)

after trying the code written above, i am getting all lines in test2 as output.

>./test.awk test1 test2

Vinod


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: How to compare 2 files using awk?

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge