Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 11-09-2011, 08:13 PM   #1
Registered: Jul 2010
Location: Earth
Distribution: Ubuntu 11.04 (Natty Narwhal)
Posts: 31

Rep: Reputation: 0
Array in awk outputs multiple values

Disclaimer: OP is 100% Awk beginner.

I use this code on ASCII files I need to report against.

   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

Model = XYZ
Manufacturer = ABC
Mfr Date = 1/1/2008
When one or more array elements are missing, output like this for instance

Model = XYZ
Manufacturer = NA
Mfr Date = NA
I have problem with the "Model" key with occasionally outputs similar entries like so

Model = XYZ
Model Number = 123456
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.

Old 11-09-2011, 11:27 PM   #2
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Try adding this in the BEGIN section
FS = " = "
The usage is explained here.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
C: testing multiple consecutive array values concurrently.? acvoight Programming 2 11-22-2010 10:23 PM
matching multiple values in awk vgr12386 Programming 3 06-15-2009 04:54 AM
C code outputs pointer references not values caged Programming 2 06-13-2005 06:24 PM
How to return values into an array using awk Helene Programming 1 05-01-2004 11:05 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:42 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration