Eduardo
Eduardo

Reputation: 397

AWK compare two files, match first field, paste data, and more

I have this script, it is used to compare the first field from two files, I want it to ignore cases, tried to add a line IGNORECASE = 1; but doesn't seem to work,

can you tell how to ignore cases?

BEGIN {
   FS=OFS=";"
}

FNR==NR {
   array[$1]=$2
   next
}

{
   if ($1 in array) { 
      print $1";" array[$1]";" $2
   }

   else {
      if ($2 in values) {
         print  $1";" "only_at_" FILENAME ";" $0 " same path    as " values[$2]
      }
      else {
         print  $1";" "only_at_" FILENAME ";" $0 " no path  found"
      }
   }
   values[$2]=$1
}

Let's say I have

File1

\\FILE48\bucan-CFAN_Subcommittees;\\SERVER24\dfs\Shared\can\CFAN Subcommittees

File2

\\file48\bucan-CFAN_Subcommittees;/fs8_100g/FILE48/BU/can/CFAN Subcommittees
\\FILE58\userhome_e;/fs1_100g/FILE58/userhome

Expected output

\\FILE48\bucan-CFAN_Subcommittees;\\SERVER24\dfs\Shared\can\CFAN Subcommittees;/fs8_100g/FILE48/BU/can/CFAN Subcommittees
\\MLISFILE58\userhome_e;only_at_file2;\\MLISFILE58\userhome_e;/fs1_100g/MLISFILE58/userhome no path found

Upvotes: 0

Views: 708

Answers (1)

glenn jackman
glenn jackman

Reputation: 246817

Pretty simple with the join command

join -t';' -i -j 1 -o 1.1,1.2,2.2 File1 File2

Using ";" as a field separator, case-insensitively join the two files on field 1, and output the first and second fields from file1 and the 2nd field from file2.

If you really want awk, this will do the same thing:

awk '
   BEGIN {FS=OFS=";"} 
   NR==FNR {key[tolower($1)] = $0; next} 
   tolower($1) in key {print key[tolower($1)], $2}
 ' file1 file2

Upvotes: 1

Related Questions