Compare 2 files and send output to other

Hi,

In File1.txt I have text like:

23AA3424DD23|3423ff25sd5345| and so on

In File2.txt I have similar text as File1, but with ",":

23aa3424dd23,192.168.1.100, and so on

I wan to remove the pipes from File1 and select 5 fields, then remove "," from File2.txt and select 2 fields (IP's and MAC's), compare (line by line) the MAC's from File2 with the ones in File1, if equal then print those 5 fields from File1 with the correspondent IP from File2 in Final.txt

I can't figure it out using awk with variables, I'm using files:

awk -F"|" '{print $1, $2, $4, $5, $10}' File1.txt > temp1.txt

awk -F"," '{print $1, $2}' File2.txt|tr '[:lower:]' '[:upper:]'  > temp2.txt

egrep -f File2.txt File1.txt > Final.txt

and I was trying to compare $2 from File2 with the awk from File1, if equal print the the whole awk from File1 adding the correspondig $1 (IP) from File2 in Final.txt

I think I'm being clear :slight_smile:

Without knowing a full example of text here is an option.

You could wrap it in a loop:

awk -F"|" '{print $1, $2, $4, $5, $10}' File1.txt|tr '[:lower:]' '[:upper:]' > temp1.txt
awk -F"," '{print $1, $2}' File2.txt|tr '[:lower:]' '[:upper:]'  > temp2.txt
for MAC in `cat temp1.txt`
 do
  MAC2=`cut -d" " -f1 temp2.txt`
  IP=`cut -d" " -f2 temp2.txt`
  if [ "$MAC" = "$MAC2" ] ; then
   echo "`cat temp1.txt` - $IP" >> Final.txt
  fi
 done

You also seem to have some lower in File1.txt so I compensated and you mentioned $2 to compare when $1 is your MAC in File2.

In File1.txt I have text like:

23AA3424DD23|3423ff25sd5345| and so on

In File2.txt I have similar text as File1, but with ",":

23aa3424dd23,192.168.1.100, and so on

If the second field from File2.txt matches the tenth field from File1.txt then write that line and append the correspondent IP from File2.txt in Final.txt

Edited: For better understanding.

Ping :slight_smile:

nawk '
BEGIN {
  FS1="|"
  FS2=","
  FS=FS2
}
FNR==NR { a[$2]=$1;next}
FNR==1 {FS=FS1;$0=$0}
$10 in a {$0=$0 FS a[$10]; print}' file2 file1

Thanks vgersh, I tested and it worked, but I had to edit the file for it to work, because the File2 is in lowercase.
I tried to insert a

tr '[:lower:]' '[:upper:]'

, can you give me a hand?

nawk '
BEGIN {
  # FieldSeparator for file1
  FS1="|"

  # FieldSeparator for file2
  FS2=","

  # start with file2 => assign the appropriate FS
  FS=FS2
}
# if processing the FIRST file (specified on cli - file2), array "a" indexed by "toupper($2)" (second field) with the value of "$1" (first field)
# "next" => go to the next record/line
FNR==NR { a[toupper($2)]=$1;next}

# if we got here - you're processing the SECOND file from cli (file1)
# if on the FIRST line (FNR==1) set the FieldSeparator (FS) to the value of FS1 "|" and reevaluate the current record ($0=$0) given a new FS
FNR==1 {FS=FS1;$0=$0}

# if the TENTH field ($10) is in array "a" (index in "a"), then print the entire record/line suffixing it with the FieldSeparator (FS) and the
# value from array "a" indexed by the TENTH field ($10).
$10 in a {print $0 FS a[$10]}' file2 file1

In order for it to work I removed the toupper in $10.

Can you give me a brief explanation of the code?

Thanks a lot, I was frying my brain :slight_smile:

updated the first code posting.

Thanks a lot!

Me again.
no luck.

What am I doing wrong?

ping :b:

if ($10 ~ /jz/) {
   $12=$12 "Text1"
   $13=$13 "Text2"
   $14=$14 "Text3"
}

Thanks

#!/bin/bash

nawk '
BEGIN {
 
  FS1=OFS="|"
  FS2=","
  FS=FS2

  suffix="ADSL" OFS "N/A" OFS "N/A"
}

FNR==NR { a[toupper ($2)]=$1;next}
FNR==1 {FS=FS1;$0=$0}

$10 in a {print $1,$2,$4,$5,$10 ($10~/^jz/)? OFS suffix : ""}' File2.txt File1.txt > Result.txt