使用 AWK 对不同文件中的多个字段进行条件匹配

Conditional matching with multiple fields comparison in different files using AWK

我再次需要您的帮助,了解如何在 2 个单独的文件中匹配 2 fields/columns,包括条件匹配记录(Employee.txt 中的状态 <> 'X' 和可用性 = 'Y' 在 Car.txt 中)。 Employee.txt($1 - 员工#,$2 - 运动)。 Car.txt($4 - 员工#,$2 - 运动)。以下是我想要实现的目标:

Employee1.txt (last column is the **status**)
1|canoeing|Sam|Smith|Seatle|X
2|jogging|Barry|Jones|Seatle|
3|football|Garry|Brown|Houston|
4|jogging|George|Bla|LA|X
5|basketball|Celine|Wood|Atlanta|
6|tennis|Jody|Ford|Chicago|

Car1.txt (last column is **availability**)
100|football|blue|5|Y
110|tennis|green|9|N
120|hockey|yellow|8|N
130|football|yellow|6|N
140|jogging|red|2|Y
150|canoeing|white|0|
    
awk -F"|" '
NR==FNR {
  if ($NF == "Y")
     car[,]
     next
}
{
    print > ($NF != "X" && (,) in car ? "match.txt" : "no_match.txt")
}' Car.txt Employee.txt

no_match.txt is the same as Employee.txt. Zero records in match.txt.

Desire output:
match.txt
2|jogging|Barry|Jones|Seatle|
5|basketball|Celine|Wood|Atlanta|

no_match.txt
3|football|Garry|Brown|Houston|
6|tennis|Jody|Ford|Chicago|

非常感谢, 乔治

注意:根据您的要求,

5|basketball|Celine|Wood|Atlanta|

不应在match.txt中,因为两个文件中的运动项目不同(足球 vs 篮球

如果您希望在“no_match.txt”中输入 status "X" 个条目:

$  awk -F"|" '                
NR==FNR { if ($NF == "Y") car[ FS ]=1; next }
{ print > ( ($NF!="X" && ( FS ) in car) ? "match.txt" : "no_match.txt") }' c.txt e.txt

结果:

kent$  head match.txt no_match.txt
==> match.txt <==
2|jogging|Barry|Jones|Seatle|

==> no_match.txt <==
1|canoeing|Sam|Smith|Seatle|X
2|jogging|Barry|Jones|Seatle|
3|football|Garry|Brown|Houston|
4|jogging|George|Bla|LA|X
5|basketball|Celine|Wood|Atlanta|
6|tennis|Jody|Ford|Chicago|

如果要排除“X”条目:

kent$  awk -F"|" '                
NR==FNR { if ($NF == "Y") car[ FS ]=1; next }
$NF!="X"{ print > (( FS ) in car? "match.txt" : "no_match.txt") }' c.txt e.txt 

结果:

kent$  head match.txt no_match.txt
==> match.txt <==
2|jogging|Barry|Jones|Seatle|

==> no_match.txt <==
3|football|Garry|Brown|Houston|
5|basketball|Celine|Wood|Atlanta|
6|tennis|Jody|Ford|Chicago|