Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two columns of two files, print the row if it matches and print zero in third column

Tags:

awk

I need to compare column 1 and column 2 of my file1.txt and file2.txt. If both columns match, print the entire row of file1.txt, but where a row in file1.txt is not present in file2.txt, also print that missing row in the output and add "0" as its value in third column.

# file1.txt #

AA ZZ   
JB  CX
CX  YZ  
BB  XX
SU  BY  
DA  XZ  
IB  KK  
XY  IK
TY  AB

# file2.txt #

AA ZZ   222
JB  CX  345
BB  XX  3145
DA  XZ  876
IB  KK  234
XY  IK  897

Expected output # output.txt #

File1.txt
AA ZZ   222
JB  CX  345
CX  YZ  0
BB  XX  3145
SU  BY  0
DA  XZ  376
IB  KK  234
XY  IK  897
TY  AB  0

I tried this code but couldn't figure out how to add rows that did not match and add "0" to it

awk 'BEGIN { while ((getline <"file2.txt") > 0) {REC[$1]=$0}}{print REC[$1]}' < file1.txt > output.txt
like image 873
Ibk Avatar asked Mar 30 '21 05:03

Ibk


People also ask

How do I compare two columns in Excel for partial matches?

One of the approaches to perform the partial match between columns is the use of the VLOOKUP function. The VLOOKUP function looks up the data in a range organized vertically. To know more about the function, visit the VLOOKUP article. We will compare the two columns and produce the result in another column.

How to compare two columns in Excel row by row?

Example 1. Compare two columns for matches or differences in the same row To compare two columns in Excel row-by-row, write a usual IF formula that compares the first two cells.

How to compare the first two columns of file2 with file1?

I would like to compare the first two columns of file2 with file1 (search through the entire contents of file1 in first two columns) if they match print the matched line of file1. Then search for the second line of file 2 and so on.

How do I compare two columns in Excel and highlight differences?

To compare two columns and Excel and highlight cells in column A that have identical entries in column B in the same row, do the following: Select the cells you want to highlight (you can select cells within one column or in several columns if you want to color entire rows).

How to compare two columns and pull the matching data?

Compare Two Columns and Pull the Matching Data If you have two datasets and you want to compare items in one list to the other and fetch the matching data point, you need to use the lookup formulas. Example: Pull the Matching Data (Exact) For example, in the below list, I want to fetch the market valuation value for column 2.


3 Answers

With your shown samples, could you please try following.

awk '
FNR==NR{
  arr[$1 OFS $2]
  next
}
(($1 OFS $2) in arr){
  print
  arr1[$1 OFS $2]
}
END{
  for(i in arr){
    if(!(i in arr1)){
      print i,0
    }
  }
}
' file1.txt file2.txt

Explanation: Adding detailed explanation for above.

awk '                    ##Starting awk program from here.
FNR==NR{                 ##Checking FNR==NR condition which will be TRUE when file1.txt is being read.
  arr[$1 OFS $2]         ##Creating array with 1st and 2nd field here.
  next                   ##next will skip all further statements from here.
}
(($1 OFS $2) in arr){    ##Checking condition if 1st and 2nd field of file2.txt is present in arr then do following.
  print                  ##Print the current line here.
  arr1[$1 OFS $2]        ##Creating array arr1 with index of 1st and 2nd fields here.
}
END{                     ##Starting END block of this program from here.
  for(i in arr){         ##Traversing through arr all elements from here.
    if(!(i in arr1)){    ##Checking if an element/key is NOT present in arr1 then do following.
      print i,0          ##Printing index and 0 here.
    }
  }
}
' file1.txt file2.txt    ##Mentioning Input_file names here.
like image 85
RavinderSingh13 Avatar answered Oct 17 '22 22:10

RavinderSingh13


You may try this awk:

awk '
FNR == NR {
   map[$1,$2] = $3
   next
}
{
   print $1, $2, (($1,$2) in map ? map[$1,$2] : 0)
}' file2 file1

AA ZZ 222
JB CX 345
CX YZ 0
BB XX 3145
SU BY 0
DA XZ 876
IB KK 234
XY IK 897
TY AB 0
like image 23
anubhava Avatar answered Oct 17 '22 22:10

anubhava


$ awk '
    { key = $1 FS $2 }
    NR==FNR { map[key]=$3; next }
    { print $0, map[key]+0 }
' file2.txt file1.txt
AA ZZ 222
JB  CX 345
CX  YZ 0
BB  XX 3145
SU  BY 0
DA  XZ 876
IB  KK 234
XY  IK 897
TY  AB 0
like image 2
Ed Morton Avatar answered Oct 17 '22 21:10

Ed Morton