LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Array in awk outputs multiple values (https://www.linuxquestions.org/questions/linux-newbie-8/array-in-awk-outputs-multiple-values-912724/)

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.

catkin 11-09-2011 10:27 PM

Try adding this in the BEGIN section
Code:

FS = " = "
The usage is explained here.


All times are GMT -5. The time now is 05:54 AM.