Home > Software design >  Conditional match when comparing fields in multiple files using AWK
Conditional match when comparing fields in multiple files using AWK

Time:12-03

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|Sam|Smith|Seatle|X
2|Barry|Jones|Seatle| 
3|Garry|Brown|Houston|X
4|George|Bla|LA|
5|Celine|Wood|Atlanta|
6|Jody|Ford|Chicago|

Car.txt (last column is **availability**)
100|red|1|Y
110|green|9|N
120|yellow|2|N
130|yellow|6|N
140|red|8|Y
150|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

CodePudding user response:

Here is how you can check for additional conditions in both files:

awk -F"|" '
NR==FNR {
   if ($NF == "Y")
      empcar[$3]
   next
}
{
   print > ($NF != "X" && $1 in empcar ? "match.txt" : "no_match.txt")
}' Car.txt Employee.txt

CodePudding user response:

With your shown samples, please try following awk code.

awk '
BEGIN{ FS=OFS="|" }
FNR==NR{
  arr[$1]=$NF
  arr1[$1]=$0
  next
}
arr[$3]!="X" && $NF=="Y"{
  print arr1[$3] > ("match.txt")
  arr2[$3]
}
END{
  for(i in arr1){
    if(!(i in arr2)){
      print arr1[i] > ("no_match.txt")
    }
  }
}
' Employee.txt car.txt

Explanation: Adding detailed explanation for above code.

awk '                                      ##Starting awk program from here.
BEGIN{ FS=OFS="|" }                        ##Setting FS and OFS to | in BEGIN section.
FNR==NR{                                   ##Checking condition FNR==NR which will be TRUE when Employee.txt is being read.
  arr[$1]=$NF                              ##Creating array arr with index of $1 and value of last field.
  arr1[$1]=$0                              ##Creating array arr1 with index of $1 and value of current line.
  next                                     ##next will skip all further statements from here.
}
arr[$3]!="X" && $NF=="Y"{                  ##Checking condition if arr array with index of 3rd column value is not X and last field is Y then do following.
  print arr1[$3] > ("match.txt")           ##Printing respective entry from Employee.txt into match.txt file here.
  arr2[$3]                                 ##Creating an entry in arr2 with index of $3 here.
}
END{                                       ##Starting END block of this awk program from here.
  for(i in arr1){                          ##Traversing through arr1 here.
    if(!(i in arr2)){                      ##Checking if i(current item index) is NOT present in arr2 then do following.
      print arr1[i] > ("no_match.txt")     ##Printing respective value to no_match.txt file.
    }
  }
}
' Employee.txt car.txt                     ##Mentioning Input_file names here.

CodePudding user response:

How about this:

awk -F'|' 'FNR==NR { empcar[$3]; next } $1 in empcar && $5 != "X"' cars emps

Output:

2|Barry|Jones|Seatle|
6|Jody|Ford|Chicago|
  • Related