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
Catalyst::TraitFor::Model::DBIC::Schema::PerRequestSchemUsermContributed Perl DocumeCatalyst::TraitFor::Model::DBIC::Schema::PerRequestSchema(3pm)

NAME
Catalyst::TraitFor::Model::DBIC::Schema::PerRequestSchema - Clone the schema with attributes for each requests SYNOPSIS
__PACKAGE__->config({ traits => ['PerRequestSchema'], }); sub per_request_schema_attributes { my ($self, $c) = @_; return (restricting_object => $c->user->obj); } ### OR ### sub per_request_schema { my ($self, $c) = @_; return $self->schema->schema_method($c->user->obj) } DESCRIPTION
Clones the schema for each new request with the attributes retrieved from your "per_request_schema_attributes" method, which you must implement. This method is passed the context. Alternatively, you could also override the "per_request_schema" method if you need access to the schema clone and/or need to separate out the Model/Schema methods. (See examples above and the defaults in the code.) SEE ALSO
Catalyst::Model::DBIC::Schema, DBIx::Class::Schema AUTHOR
See "AUTHOR" in Catalyst::Model::DBIC::Schema and "CONTRIBUTORS" in Catalyst::Model::DBIC::Schema. COPYRIGHT
See "COPYRIGHT" in Catalyst::Model::DBIC::Schema. LICENSE
This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.14.2 2012-06-12 Catalyst::TraitFor::Model::DBIC::Schema::PerRequestSchema(3pm)
All times are GMT -4. The time now is 06:55 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy