Computing.Net > Forums > Programming > awk program

awk program

Reply to Message Icon

Original Message
Name: ssonu
Date: August 27, 2007 at 10:26:12 Pacific
Subject: awk program
OS: OS/2
CPU/Ram: p4
Comment:

Hi
I'm a begginer to awk. I need to know how to compare 2 files with similar data, just that file 1 holds few records as when file 2 holds many records.

File 1:

Arnold Kim
Mike Parris
Kris Shay

File 2:

Arnold Kim
Kevin Parker
Mike Parris
Ben Scott
Kris Shay

If the records match in these two files I wanna generate an output file
(file 3, with the matched records)
Arnold Kim
Mike Parris
Kris Shay

and another file (file 4 with unmatched data).

Kevin Parker
Ben Scott
Can you help me out.

ss


Report Offensive Message For Removal


Response Number 1
Name: nails
Date: August 27, 2007 at 16:17:36 Pacific
Reply: (edit)

Are you sure this isn't homework? Anyway, take a look at this link

http://www.computing.net/unix/wwwbo...

James Booth shows you how to read a file into an awk array.


Report Offensive Follow Up For Removal

Response Number 2
Name: ghostdog
Date: August 27, 2007 at 23:20:04 Pacific
Reply: (edit)

Since you are a beginner, you should be reading up on awk, trying to do it yourself first before even posting in any forums. however, giving you the benefit of doubt, here's one way in awk.

[code]
awk 'FNR==NR{ arr[$0];next}
{if ($0 in arr) print $0 > "matched.txt"
else print $0 > "not_matched.txt"
}' "file" "file1"
[/code]


Report Offensive Follow Up For Removal

Response Number 3
Name: Guy
Date: August 29, 2007 at 14:18:12 Pacific
Reply: (edit)

It sounds like homework to me.

If you are really running OS2, you should be doing this in REXX, not Awk.


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: awk program

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 6 Days.
Discuss in The Lounge