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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

question about using arrays

Status
Not open for further replies.

dido78

Technical User
Aug 10, 2007
1
CZ
Can someone explain to me why the following code first prints an unfiltered output then the filtered one? Thanks.

df -k | /usr/xpg4/bin/awk -v maxpct=$1 '/^[^F]/

{ array[i++] = $6; arr3[j++] = $3; arr2[k++] = $2;}

END {
for ( i=1 ; i<=j; i++ )
{
if ( arr2 != 0 )
arraypct = ( arr3 / arr2 * 100 )
else
arraypct = 0
if ( arraypct > maxpct )
print array"\n", arraypct"\n\n"
}

}

'
 
Take out the carriage return after /^[^F]/. awk expects the code that follows the expression to be immediately after it on the same line. You could use this layout if you prefer:

[tt]/^[^F]/ {
array[i++] = $6; arr3[j++] = $3; arr2[k++] = $2;
}[/tt]


Annihilannic.
 
To explain further... the default action awk takes if it finds a matching expression and no code after it is to simply print the line, hence the output you were getting.

Additionally the code you had on a separate line would be executed for every line of input, because no expression was specified before it.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top