Sponsored Content
Top Forums Shell Programming and Scripting Array in awk outputs multiple values Post 302572368 by alan on Wednesday 9th of November 2011 08:15:10 PM
Old 11-09-2011
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.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

saving values from awk expression into shell array

hi i am trying to save the values i extract from a file with the help of awk in a bash shell array. i have: exec 10<file2 while read LINE <&10; do ARRAY1=$(awk '{print $1}' file2) ((count++)) done echo ${ARRAY1} it prints just blank lines. file1 has two columns and i... (4 Replies)
Discussion started by: npatwardhan
4 Replies

2. Shell Programming and Scripting

saving values in file in an array in awk

hi i am trying to save values in a file in an array in awk..the file is as follows: 0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0, so far i have this: awk 'BEGIN {RS="\n";FS=","} { for(i=1;i<=NR;i++) { for(j=1;j<=NF;j++) { a=$j; } } (4 Replies)
Discussion started by: npatwardhan
4 Replies

3. Shell Programming and Scripting

fetching values using awk and storing into array

hi all I am using awk utility to parse the file and fetching two different vaues from two different record of a record set. I am able to see the result, now i want to store the result and perform some check of each values form database to mark valid and invalid. could you please help me... (3 Replies)
Discussion started by: singhald
3 Replies

4. Shell Programming and Scripting

Problem with lookup values on AWK associative array

I'm at wits end with this issue and my troubleshooting leads me to believe it is a problem with the file formatting of the array referenced by my script: awk -F, '{if (NR==FNR) {a=$4","$3","$2}\ else {print a "," $0}}' WBTSassignments1.txt RNCalarms.tmp On the WBTSassignments1.txt file... (2 Replies)
Discussion started by: JasonHamm
2 Replies

5. Shell Programming and Scripting

How to store multiple outputs from an awk command?

x=`echo $line | awk -F "|" '{print $1;print NR}'` How will I get the 2 return values ($1 and NR) from awk to variables? (4 Replies)
Discussion started by: tene
4 Replies

6. Shell Programming and Scripting

Select multiple values from an Oracle database and assign it to two dimensional array

hi I have two tables in oracle DB and am using a joining query which will result in the output as follows. i need to assign it to a two dimensional array and use it for my further calculations. the way i tried is as follows. #!/bin/ksh export... (1 Reply)
Discussion started by: aemunathan
1 Replies

7. Shell Programming and Scripting

Piping Unix Variable Array values into AWK

#ksh Here is my code: ERRORLIST="43032 12001 12002 12003 12004 34019 49015 49016 49017 49018 49024 49025 49026 58004 72003 12005 12006 12007 12008 12011 12012 16024 16023" for ERROR in ${ERRORLIST} do awk -v l="$lastdate" '/^....-..-../&&$0>l{d=$0}d&&/Error: '"${ERROR}"'/{print... (3 Replies)
Discussion started by: k1ko
3 Replies

8. Shell Programming and Scripting

storing multiple values in a array variable

Am using a find command in my script .The output may be one or more. I need to store those values in a array and need to access those. Am unable to find the solution . Any help on this will be helpful. if < code> else a=<find command output which gives the file name either 1 or more> if 1... (1 Reply)
Discussion started by: rogerben
1 Replies

9. Shell Programming and Scripting

Assigning array values using awk in shell scripting

hi My script as below #!/bin/ksh for i in `seq 1 7` do a=$(awk '{print $i}' /home/rama/expenese.txt) done for i in `seq 1 7` do echo "${a}" done content of expense.txt is as below 5032 210179 3110 132813874 53488966 11459221 5300794 I want output as... (6 Replies)
Discussion started by: Ramakrishna V
6 Replies

10. UNIX for Advanced & Expert Users

Comparing wc outputs using array

Hi All, Im trying to compare the wc -l output with another set of rowcount outputs which returned from sql... For Eg : Im storing the first outputs as below <srccnt=`wc -l $HOME/*.csv | awk {'print $1'}` and comparing this with the another set of outputs. descnt=`seclect count(*)... (7 Replies)
Discussion started by: Deena1984
7 Replies
Config::Model::Backend::Fstab(3pm)			User Contributed Perl Documentation			Config::Model::Backend::Fstab(3pm)

NAME
Config::Model::Backend::Fstab - Read and write config from fstab file VERSION
version 2.021 SYNOPSIS
No synopsis. This class is dedicated to configuration class "Fstab" DESCRIPTION
This module is used directly by Config::Model to read or write the content of a configuration tree written with "fstab" syntax in "Config::Model" configuration tree. Typically this backend will be used to read and write "/etc/fstab". Comments in file_path This backend is able to read and write comments in the "/etc/fstab" file. STOP
The documentation below describes methods that are currently used only by Config::Model. You don't need to read it to write a model. CONSTRUCTOR
new ( node => $node_obj, name => 'fstab' ) ; Inherited from Config::Model::Backend::Any. The constructor will be called by Config::Model::AutoRead. read ( io_handle => ... ) Of all parameters passed to this read call-back, only "io_handle" is used. This parameter must be IO::File object already opened for read. It can also be undef. In this case, "read()" will return 0. When a file is read, "read()" will return 1. write ( io_handle => ... ) Of all parameters passed to this write call-back, only "io_handle" is used. This parameter must be IO::File object already opened for write. "write()" will return 1. AUTHOR
Dominique Dumont, (ddumont at cpan dot org) SEE ALSO
Config::Model, Config::Model::AutoRead, Config::Model::Backend::Any, perl v5.14.2 2012-11-09 Config::Model::Backend::Fstab(3pm)
All times are GMT -4. The time now is 03:36 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy