Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing and formatting

Status
Not open for further replies.

suxen

ISP
Apr 7, 2003
1
FI
Not sure at all that the following is even possible to do with awk, but...
I have two files file1 and file2 with to columns x and y in both.
The file1 is "base" and I want to compare column x from file2 to file1 and then print only those lines from file2 that are found also from file1. (where x2 == x1)

Problem is also that the columns aren't in the same order.

file1 file2

x1 y1 x3 y13
x2 y2 x1 y11
x3 y3 x4 y14

Print: x1 y11
x3 y13
 
I know it's not awk ...
[tt]
sort file1 > file3
sort file2 > file4
join -o 1.1,2.2 file3 file4
[/tt]
but it does work ...
[tt]
x1 y11
x3 y13
[/tt]
 
Here is an awk solution.

BEGIN {
if (!fn) fn="file2"
while ((getline < fn) > 0) a[$1] = $0
}
{
if (a[$1]) print a[$1]
} CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top