1

My code 1 :

awk -F'|' -v PARM_VAL="${PARM_VALUE[*]}" '
BEGIN { split(PARM_VAL,pa," ") 
fn_1()
{
print "inside fn"
}
}
FNR==NR{ for(i=1;i<=NF;i++) a[NR,i]=$i; }

{if (FILENAME == "SPP_OUT") {print $1}}
fn_1 
END {printf " second value of SPPIN : "a[2,2]} ' SPP_IN SPP_OUT

I am getting error fatal: function `fn_1' not defined

My code 2 :

awk -F'|' -v PARM_VAL="${PARM_VALUE[*]}" '
BEGIN { split(PARM_VAL,pa," ") 
fn_1()
{
ret = "returned"
return ret
}
}
FNR==NR{ for(i=1;i<=NF;i++) a[NR,i]=$i; }

{if (FILENAME == "SPP_OUT") {print $1}}
m=fn_1()
END {printf " second value of SPPIN : "a[2,2];print $m} ' SPP_IN SPP_OUT

I am facing awk: cmd. line:6: return ret awk: cmd. line:6: ^ `return' used outside function context

Can any asssist ? Thanks

5
  • isn't this a duplicate of stackoverflow.com/questions/28735013/… ? Commented Feb 26, 2015 at 7:56
  • Try to define the function before BEGIN statement as like "awk -F'|' -v PARM_VAL="${PARM_VALUE[*]}" ' fn_1() { print "inside fn" } BEGIN { split(PARM_VAL,pa," "); } FNR==NR{ for(i=1;i<=NF;i++) a[NR,i]=$i; } {if (FILENAME == "SPP_OUT") {print $1}} fn_1 END {printf " second value of SPPIN : "a[2,2]} ' SPP_IN SPP_OUT" Commented Feb 26, 2015 at 7:56
  • @ candymanuu This issue is related to function Commented Feb 26, 2015 at 8:01
  • @vijayalakshmi-d Tried. Still facing same issue Commented Feb 26, 2015 at 8:13
  • @vijayalakshmid you can define a function anywhere in the code outside of a condition or action block. Before BEGIN, after BEGIN, after END,.... wherever. Commented Feb 26, 2015 at 12:36

2 Answers 2

4

The function should be defined out of the BEGIN block. For example:

$ cat function.awk
function fib(n,  n_1, n_2)
{
    if (n < 2) {
        return n
    } else {
        n_1 = fib(n - 1)
        n_2 = fib(n - 2)
        return n_1 + n_2
    }
}

BEGIN {
    for (i = 0; i < 5; ++i) {
        printf("fib(%d) = %d\n", i, fib(i));
    }
}
$ awk -f function.awk
fib(0) = 0
fib(1) = 1
fib(2) = 1
fib(3) = 2
fib(4) = 3
$

The user-defined awk function syntax is

function NAME(PARAMETER-LIST)
{
    BODY-OF-FUNCTION
}

The tricky part is:

PARAMETER-LIST is a list of the function's arguments and local variable names, separated by commas. When the function is called, the argument names are used to hold the argument values given in the call. The local variables are initialized to the empty string. A function cannot have two parameters with the same name, nor may it have a parameter with the same name as the function itself.

See the awk manual for more details.

Sign up to request clarification or add additional context in comments.

Comments

2

To get the nucleus of your code working I had to add the function keyword when defining the function, and parentheses when making the call like so:

$ cat foo.awk
BEGIN { print "begin" }

function fn_1()
{
  print "inside fn"
}

{
    fn_1()
}

END { print "end" }

$ echo 'xyz' | awk -f foo.awk
begin
inside fn
end

From the awk manual:

The definition of a function named name looks like this:

function name(parameter-list)
{
body-of-function
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.