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:
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
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
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