Issue
I would like to find out how/whether it is possible to include some condition while comparing multiple fields from different files in AWK on AIX 6.x platform. Below is what I am trying to do:
Employee.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|basketballCeline|Wood|Atlanta|
6|tennis|Jody|Ford|Chicago|
Car.txt (last column is **availability**)
100|football|red|1|Y
110|tennis|green|9|N
120|hockey|yellow|2|N
130|football|yellow|6|N
140|jogging|red|8|Y
150|canoeing|white|0|
awk -F"|" '
NR == FNR {
empcar[$3]
next
}
{
print > ($1 in empcar ? "match.txt" : "no_match.txt")
}' Car.txt Employee.txt
I like to check if the employee status is Active (no X) and the same for car availability (Y) before printing the matched record. Is this doable?
Thanks a lot, George
Solution
Here is how you can check for additional conditions in both files:
awk -F"|" '
NR==FNR {
if ($NF == "Y")
empcar[$(NF-1)]
next
}
{
print > ($NF != "X" && $1 in empcar ? "match.txt" : "no_match.txt")
}' Car.txt Employee.txt
Answered By - anubhava Answer Checked By - Terry (WPSolving Volunteer)