Define procedure in block END in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Define procedure in block END in awk
# 1  
Old 12-01-2013
Define procedure in block END in awk

Hi Smilie

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

Code:
BEGIN {i=1}
{
 if  ($1 == $2)
  cadena[i] = $3
}

END {
find_letter(cadena[i])
}

find_letter(cadena[i]
{
 ...
}

}

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 ...

Code:
BEGIN {i = 1}
{
  if ($ 1 == $ 2)
   cadena [i] = $ 3
}

END {
find_letter (cadena[i])
}

find_letter (cadena[i])
{
  ...
}

}

Running it gives me the following error

the 'find_letter' is undefined

Thanks for advance.

Last edited by Don Cragun; 12-01-2013 at 10:14 PM.. Reason: Add CODE tags.
# 2  
Old 12-01-2013
Quote:
Originally Posted by solaris21
I would like to know if you can define procedures within the END block.
No. Function definitions are top-level only.

Regards,
Alister
# 3  
Old 12-01-2013
Quote:
Originally Posted by solaris21
Hi Smilie

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

Code:
BEGIN {i=1}
{
 if  ($1 == $2)
  cadena[i] = $3
}

END {
find_letter(cadena[i])
}

find_letter(cadena[i]
{
 ...
}

}

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 ...

Code:
BEGIN {i = 1}
{
  if ($ 1 == $ 2)
   cadena [i] = $ 3
}

END {
find_letter (cadena[i])
}

find_letter (cadena[i])
{
  ...
}

}

Running it gives me the following error

the 'find_letter' is undefined

Thanks for advance.
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:
Code:
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.

Last edited by Don Cragun; 12-01-2013 at 10:48 PM.. Reason: Add note re Solaris awk versions.
# 4  
Old 12-02-2013
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Increment with awk - how to define start value

Hello, I am running under ubuntu18.04 My question is about awk. inputfile 0wo010011oasasds sdjhsdjh=, u12812888 8jsjkahsjajnsanakn akjskjskj=, suhuhuhwx kskkxmsnnxsnjxsnjxsnjjnjjdi=, 22878ssssss Below code adds consecutive numbers when string = is found run_code: awk -F'=' -v OFS='='... (4 Replies)
Discussion started by: baris35
4 Replies

2. Shell Programming and Scripting

Search a String between start and end of a block in a file

Hi, I have a scenario where I want to display the output based on the pattern search between the start and end of a block in a file, we can have multiple start and end blocks in a file. Example give below, we need to search between the start block abc and end block def in a file, after that... (5 Replies)
Discussion started by: G.K.K
5 Replies

3. UNIX for Dummies Questions & Answers

Add a block of code at the end of a specific block

I need to search for a block with the starting pattern say "tabId": "table_1", and ending pattern say "]" and then add a few lines before "]" "block1":"block_111" "tabId": "table_1", "title":"My title" ..... .... }] how do I achieve it using awk and sed. Thanks, Lakshmi (3 Replies)
Discussion started by: Lakshmikumari
3 Replies

4. UNIX for Dummies Questions & Answers

sed - Add a variable line to the end of a block beginning with a regex

Hi, Need some help with sed. I have a file that has sections : e.g. a=blah b=blah d=blah e=blah There's many sections in the file. (1 Reply)
Discussion started by: andyatit
1 Replies

5. Shell Programming and Scripting

Define Positional Parameter Range Awk

Hello All, I am trying to clean up a poorly looking awk command. I am searching for a way to define a range of positional parameters. I may not be searching for the correct syntax. Example: awk ' /14:3*/ {print $2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13}' app.log Is it possible to shorten... (4 Replies)
Discussion started by: jaysunn
4 Replies

6. Shell Programming and Scripting

array access in END block failure

Hi guys i am new to shell scripting. I wrote this script that simply searches a column value of file1 from file2. please look at the code below: awk ' FILENAME==ARGV { file_1_data=$0; next } FILENAME==ARGV { file_2_data=substr($3,1,12); next } END { ... (5 Replies)
Discussion started by: fahadaizaz
5 Replies

7. Shell Programming and Scripting

Define an alias with an embeded awk command ??

Hi all, I'm trying to define an alias with an embeded awk command: alias kpipe='kill `psme| grep "ifw -r" | grep -v "grep ifw -r"| awk '{print $2}'`' The problem is that the awk command (awk '{print $2}') contains two ' ..' quotes. So bash assumes that the first awk quote corresponds to... (5 Replies)
Discussion started by: jfortes
5 Replies

8. Shell Programming and Scripting

if clause in AWK END block not working.

Hello all... I have a slight problem in my awk script... I have a script which checks a csv file and keeps a count of any invalid records and then if it finds any, exits with a code of 1. problem is it dosnt seem to work properly :rolleyes: Everthing seem to work interms of the stats output,... (1 Reply)
Discussion started by: satnamx
1 Replies
Login or Register to Ask a Question