Error using function in AWK code


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Error using function in AWK code
# 1  
Old 10-25-2011
Error using function in AWK code

Hi to all,

I have an AWK code that looks like this:
Code:
awk 'NR==FNR
        {
        #code to process file1 
         next
        }
        {
        #code to process file2 
        a=myfunction(x)
        }
        END {
            #Some code
             b=myfunction(x)
             
            }'file1 file2

As you can see, I need to call at least one time myfunction() when NR !=FNR and inside END{} statement.

I don't know where is the correct place to insert the function within the AWK code. If I put the function code in the part
when NR==FNR I receive errors and when I put the function in the part when NR !=FNR I receive the same error.

The function is a recursive function and I receive this error.
Code:
awk: cmd. line:4: function recurse(number,   ret)
awk: cmd. line:4: ^ syntax error
awk: cmd. line:16: return ret
awk: cmd. line:16: ^ `return' used outside function context

Where is the correct place put the function code?

Many thanks in advance
# 2  
Old 10-25-2011
awk

Hi,

Try to use function definition in BEGIN block cos your code calls the function before function definition i think.
END block execute at the end right.

Cheers,
RangaSmilie
# 3  
Old 10-25-2011
Probably best to define function up front:

Code:
awk '
  function recurse(number,   ret) {
        # Some code
        return ret
  }
  NR==FNR {
        #code to process file1 
         next
  }
  {
        #code to process file2 
        a=myfunction(x)
  }
  END {
        #Some code
        b=myfunction(x)
 
  }'file1 file2

# 4  
Old 10-25-2011
Hi rangarasan and Chubler,

Thanks for your reply.

I've tried both and Chubler's suggestion worked for me.

Many thanks for your help guys.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. Shell Programming and Scripting

awk function

hi, I have used awk command to delimit my variable. But this that not worked. Could you please let me know what need to be changed in my awk command Input: home/unix>cat test.txt DAILY.JOB CHENNAI,8388 DAILY.JOB BANGLORE,3848 DAILY.JOB TRICHY,9489 DAILY.JOB TIRUPUR,8409 code ... (9 Replies)
Discussion started by: arun888
9 Replies

3. Shell Programming and Scripting

awk function

Hi all, I need to have informations in a URL : https://www.autolib.eu/stations/ Valors I need are in bold: {"charging_status": "nonexistent", "rental_status": "future", "subscription_status": "nonexistent", "station_id": 791, "address": "10 rue de Rome, 93110 Rosny-sous-Bois", "lat":... (3 Replies)
Discussion started by: roulitto
3 Replies

4. UNIX for Advanced & Expert Users

error:- sh: error importing function definition for `module

Hi, We have installed linux6(RHEL) OS and installed datastage application on that. First time installation worked fine and our all services related to datastage was up and running. When we stopped the datastage and restarted its giving below error while restart:- ./uv -admin -start ... (0 Replies)
Discussion started by: prasson_ibm
0 Replies

5. Shell Programming and Scripting

Can someone verify the code of two function

Input=D123,S1234,D345 | kESTREL PRODUCTION SUPPORT echo -en "Enter the logmsg=" read logmsg logmsg1=${logmsg%%|*}; echo "$logmsg1"|tr ',' '\n' | sed 's/*$ | sed '/^$/d'//'>pre-commit.config Char() { while read line do if ] || ];then echo "Success" else exit 1;... (1 Reply)
Discussion started by: rohit22hamirpur
1 Replies

6. Shell Programming and Scripting

awk: Internal software error in the tostring function on

Hello, I posted a working script on this thread: https://www.unix.com/emergency-unix-linux-support-help-me/160123-help-make-awk-script-more-efficient-large-files.html When I run this script on a large file, 351 MB I get this error: awk: Internal software error in the tostring function on... (8 Replies)
Discussion started by: script_op2a
8 Replies

7. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

8. Shell Programming and Scripting

awk function from shell

Hi all. I have a selection of awk functions which I have written and tested in a main awk program. From the command line I use nawk -f function -f main to run main and call my functions from within it. I now wish to use these awk function again but want to call them from inside an awk section of... (1 Reply)
Discussion started by: pxy2d1
1 Replies

9. Shell Programming and Scripting

Help with the function awk

Hi I am trying to create a modify a txt file via a sh script and I'm not sure how to do it. I have this: data1a#data2a#data3aµ data1b#data2b#data3bµ data1c#data2c#data3cµ and I want to have this (more or less) data1a data2a data3a data1b data2b data3b data1c data2c data3c I know... (5 Replies)
Discussion started by: Morgwen
5 Replies

10. Shell Programming and Scripting

awk with function ?? please, help :(

Here is my test.in file Case Modify 10001 20002 30003 40004|Report Create 3417176211|Case Modify 10002 20002 30003 40004| Script: Remove.ksh This script to remove $1 which I type in: $ cat test.in Case Modify 10001 20002 30003 40004|Report Create 3417176211|Case Modify 10002 20002 30003... (0 Replies)
Discussion started by: sabercats
0 Replies
Login or Register to Ask a Question