LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 11-09-2011, 07:13 PM   #1
kristo5747
Member
 
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.

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.
 
Old 11-09-2011, 10:27 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

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


Reply


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 09:23 PM
matching multiple values in awk vgr12386 Programming 3 06-15-2009 03:54 AM
C code outputs pointer references not values caged Programming 2 06-13-2005 05:24 PM
How to return values into an array using awk Helene Programming 1 05-01-2004 10:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:54 PM.

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