Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to detect missing elements in awk array

Status
Not open for further replies.

emblem

Programmer
Jun 25, 2002
26
Hi all:

I have an awk array which looks like this:

5
2
1
4


I.e, up to n values, where each n is a positive integer <= n

How can I tell that I am missing the value 3 ??

Here is my code:

fullcount_p=16

ps -ef|grep PSAPPSRV | egrep -v '(grep|RPSHR)'| /usr/xpg4/bin/awk -v fcp=$fullcount_p '{P_ary[NR] = $14}
END {if ( $NR -lt fcp )
{for (j = 0; j <(fcp - 1); j++)
{if ( P_ary[j] == '' )
print j
}
}
}

}'

This dies on the print j line, cause it treats the '' as a missing right-hand operand. What I need is an ISNULL statement. Here is the error message:

/usr/xpg4/bin/awk: file &quot;[command line]&quot;: line 3: syntax error Context is:
>>> = 0; j <(fcp - 1); j++)
>>> {if ( P_ary[j] == ) <<<


I realize the egrep is lame, but my shell doesn't have a -e flag for grep.

I suspect that a workaround for my problem is to initialize an array with fullcount_p elements in BEGIN, then delete elements for every matching input, then print just the remaining elements in the END, but if you know how to identify null elements directly, that would be much appreciated!
 
if ( !(j in P_ary) )

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
You have a syntax error:
{if ( P_ary[j] == '' )
instead of:
{if ( P_ary[j] == &quot;&quot; )

Hope This Help
PH.
 
Also, you seem to be looping from 0 thru fcp-2, so maybe use...

for ( j=1 ; j <= fcp ; j++ )
 
also:

> if ( $NR -lt fcp )

should probably be

if ( NR < fcp )

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks for the help, guys. All advice was on the money
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top