BEGIN {FS="," } /^$/ {next} #first-pass skip trivial blanks { # second-pass skip trivial blanks lines if ("" == $2) next; # DtoX for cross-platform linefeeds (the "^M" is an actual #13 or \015 character) {if (1 < split($NF,dd,"\ ")) $NF=dd[1]; } # make-canonical the nickname quoting (saves 18) -- drop out quotes, we'll add them back in when we print gsub("\"","",$2); # trivial strcasecmp() dupe; skip (saves 4) if ((toupper($2) == toupper(HOLD_NAME)) && (toupper($1) == toupper(HOLD_WWN))) { next } # search for duplicate Alias if ($2 == HOLD_NAME) { # grab last 16 bits of WWN for uniqueness wwn1c=split(HOLD_WWN,wwn1,":"); wwn2c=split($1,wwn2,":"); # check that the last 16 bits of the WWN are unique enough if ((wwn1c == wwn2c) && ((wwn1[7] != wwn2[7]) || (wwn1[8] != wwn2[8]))) { # only print one here, shift the other into HPR_NAME HPR_NAME = HOLD_NAME "-" wwn1[7] wwn1[8]; printf "%s,\"%s\"\n",HOLD_WWN,HPR_NAME; HOLD_WWN=$1; HOLD_NAME=$2; HPR_NAME=$2 "-" wwn2[7] wwn2[8]; next; # so now HOLD_NAME would look like LTO_tape_drive9, HPR_NAME would look like LTO_tape_drive9-0949, HOLD_WWN has 10:00:00:00:c9:32:09:49 # in essence HOLD_NAME is used for comparison, but HPR_NAME is used for printing when the time comes # we do this game of pushing to HOLD_ rather than directly printing to give us this "wait, lemme fix that name" opportunity } # otherwise complain if the WWN are "too similar" else { if ("" != CORRECTIONFILE) printf "dupe: %s used for %s and %s\n",HOLD_NAME, HOLD_WWN, $1 > CORRECTIONFILE; else printf "dupe: %s used for %s and %s\n",HOLD_NAME, HOLD_WWN, $1; printf "%s,\"%s\"\n",HOLD_WWN,HPR_NAME; HOLD_WWN=$1; HOLD_NAME=$2; HPR_NAME=$2; next; } } # (implicit "else" due to "next" in previous block) # search for duplicate WWN (which is ensured to be just before or after due to the "sort" or "sort.exe" used) if ($1 == HOLD_WWN) { # don't really have a solution for two names on a WWN... we removed trivial dupes (case, spacing) above... if ("" != CORRECTIONFILE) printf "dupe: WWN %s appears twice for nicknames %s and %s\n", HOLD_WWN, HOLD_NAME, $2 > CORRECTIONFILE; else printf "dupe: WWN %s appears twice for nicknames %s and %s\n", HOLD_WWN, HOLD_NAME, $2; printf "%s,\"%s\"\n",HOLD_WWN,HPR_NAME; HOLD_WWN=$1; HOLD_NAME=$2; HPR_NAME=$2; next; } # below here all prints should be followed by nexts # most cases should fall through to this since 90% are not dupes. # the if simply avoids the result of the first line having nothing held in the HOLD_* if ("" != HOLD_WWN) printf "%s,\"%s\"\n",HOLD_WWN,HPR_NAME; HOLD_WWN=$1; HOLD_NAME=$2; HPR_NAME=$2; } END { printf "%s,\"%s\"\n",HOLD_WWN,HPR_NAME; HOLD_WWN=$1; HOLD_NAME=$2; }