Define procedure in block END in awk

Hi :slight_smile:

Yo quisiera saber si se puede definir procedimientos dentro del bloque END.
for example ...

BEGIN {i=1}
{
 if  ($1 == $2)
  cadena = $3
}

END {
find_letter(cadena)
}

find_letter(cadena
{
 ...
}

}

Al ejecutarlo me da el siguiente error

la funci�n �find_letter� no est� definida

Thanks for advance.

---------- Post updated at 08:04 PM ---------- Previous update was at 07:59 PM ----------

please me excuse, here this the test in English:

I would like to know if you can define procedures within the END block.
for example ...

BEGIN {i = 1}
{
  if ($ 1 == $ 2)
   cadena  = $ 3
}

END {
find_letter (cadena)
}

find_letter (cadena)
{
  ...
}

}

Running it gives me the following error

the 'find_letter' is undefined

Thanks for advance.

No. Function definitions are top-level only.

Regards,
Alister

It is interesting that your Spanish awk code is very different from your English awk code, neither of them defines any function, both of them have a "}" with no matching "{", and the Spanish version has a "(" with no matching ")".

The way to define a function in awk is to use:

function function_name([parameter, ...]) { statements }

You didn't have the word function (which is required in a function definition) anywhere in your awk scripts.

And, when you invoke a user-defined function, there can't be any space between function_name and the following open parenthesis (which you have in the English version of your script). The English version of your script also has lots of other extra spaces that shouldn't be there (between "$" and the field number following it, between an array name and the following "[".

You can define a function in an awk script anywhere you can have a pattern{action} pair (which includes before or after a BEGIN{action} or before or after an END{action} . A function definition cannot appear as part of the action in a BEGIN or END pattern{action} pair.

PS. If you intend to write an awk script on a Solaris system (guessing from your login name), use /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk instead of /bin/awk or /usr/bin/awk .

Hi Don Cragun:

You apologize to me, I will follow the rules of that foro.
I undestand, It isn't possible define a user-defined function within the END block, thank you for helping.

I started using OpenSolaris in 2009, Now I use Kubuntu and OpenSuse.

Regards.