Are you looking for a match on any substring of the 2nd field in file02.out , or are you looking for an exact match on the first three components of the IP address in the 2nd field in file02.out ?
Thanks for spotting that error, I corrected it. I intended to have as per your example. I am looking for an exact match on the first three components on the second field.
awk -F, ' # Invoke awk to process your files with the field
# separator set to ",".
FNR == NR { # For lines in the 1st input file (the # of records in
# the current file is the same as the # of records in
# all files read so far)...
ip[$1 "."] # Add an element to the array ip[] with the index being
# the text found in the 1st field from the first file
# with a "." added to the end of that string.
next # Stop processing this input record and read the next
# input line.
}
{ for(i in ip) # For lines read from any following input files, loop
# through all of the index values that have been used in
# the array ip[], with "i" set to a different index each
# time through the loop...
if(index($2, i) == 1) { # if the index for this time through the
# loop identically matches a substring
# starting with the 1st character of the
# 2nd field in this file...
print # print the current input line...
next # and stop processing this line and read the
# next input line
}
}' file0[12].out # End the script and name the two input files.
If someone else wants to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .
Note that I used index() rather than using an ERE match because the period is a special character in an ERE that matches any character (not just a period), so setting up the index when adding elements to the ip[] array would have been more difficult:
ip[$1 "."]
would need to be something more like:
gsub(/[.]|$/, "[.]", $1)
ip["^" $1]
but the match in the loop:
if(index($2, i) == 1) {
would have been simpler (but would probably also run slower):