kristo5747 |
11-09-2011 07:13 PM |
Array in awk outputs multiple values
Disclaimer: OP is 100% Awk beginner.
I use this code on ASCII files I need to report against.
Code:
awk 'BEGIN {
tokens["Model"] = 0
tokens["Manufacturer"] = 0
tokens["Mfr Date"] = 0
}
{ for (token in tokens)
{ if ($1 == token){print $0; tokens[$1]++;}}}
END {for (token in tokens){
if( tokens[token] == 0){printf("%-13s = NA\n" , token)}}}' FILENAME
Most of my files output like this
Code:
Model = XYZ
Manufacturer = ABC
Mfr Date = 1/1/2008
When one or more array elements are missing, output like this for instance
Code:
Model = XYZ
Manufacturer = NA
Mfr Date = NA
I have problem with the "Model" key with occasionally outputs similar entries like so
Code:
Model = XYZ
Model Number = 123456
HW Model = AAABBBCCC
Manufacturer = NA
Mfr Date = NA
Is there a way in Awk to specify the token "Model" exclusively so that anything else is ignored? I tried "$Model^" as a key but to no avail.
TIA.
|