BEGIN {
## The field-separator is a tab.
FS = "\t"
## Make a way to get file-number from filename.
for (i=1; i<ARGC; i++)
filenum[ARGV[i]] = i
}
{
whichfile = filenum[FILENAME]
# Store all lines of all files.
lines[whichfile,FNR] = $0
if (1==whichfile)
{ # Save key to match with file2.
keys[2,grab167()]++
# Save key to match with file3.
keys[3,grab145()]++
}
else # File 2 or 3.
{ key = (2==whichfile) ? grab167() : grab145()
# If this key was in file1...
if ((whichfile,key) in keys)
# and no uid has yet been assigned...
if (!((whichfile,key) in uidmap))
{ uid[whichfile]++
uidmap[whichfile,key] = uid[whichfile]
}
}
}
END {
# Print file1.
for (line=1; (1,line) in lines; line++)
{ $0 = lines[1,line]
display( uidmap[2,grab167()], uidmap[3,grab145()] )
}
for (file=2; file<=3; file++)
{ print "____ file " file
for (line=1; (file,line) in lines; line++)
{ $0 = lines[file,line]
if (2==file)
display( uidmap[2,grab167()], 0 )
else
display( 0, uidmap[3,grab145()] )
}
}
}
function display( id1, id2 ,id)
{ id = form_id( id1 ) " " form_id( id2 )
printf "%s %s\t%s-%s-%s\n", id,
$0,substr($6,1,3),substr($5,1,3),substr($4,1,4)
}
function form_id( id )
{ if (id)
return sprintf( "%03d", id )
return " "
}
# For file2.
function grab167( )
{ return clean1($1) SUBSEP clean567($6) SUBSEP \
clean567($7)
}
# For file3.
function grab145( )
{ return clean1($1) SUBSEP $4 SUBSEP clean567($5)
}
function clean567( s )
{ return substr( s, 1, 3)
}
function clean1( s )
{ gsub ( /[-\/_() ]/, "", s )
if ( s ~ /[0-9](E|X)$/ )
sub( /(E|X)$/, "EXT", s )
return s
}