Calling function in awk statement.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calling function in awk statement.
# 1  
Old 03-31-2010
Calling function in awk statement.

Hi All,

I have an awk statement and a function defined in a script.

I am trying to call the function from inside awk statement, i.e.

Code:
awk ' myFunk () ;' filename

But when I define myFunk() before awk, then I receive this error:
Code:
s2.sh: line 48: syntax error: unexpected end of file

and when I define it after awk, ofcourse it says myFunk() not defined and similar.

Could you please guide on how to call this function from awk statement?

Best Regards..
# 2  
Old 03-31-2010
Can you explain what you're trying to achieve? You can't call a shell function like that with awk.
# 3  
Old 03-31-2010
Why do you need to do that? Pipe the result of the function to awk
Code:
myFunk inputfile | awk '{ do awk things here}'

# 4  
Old 03-31-2010
Quote:
Originally Posted by jim mcnamara
Why do you need to do that? Pipe the result of the function to awk
Code:
myFunk inputfile | awk '{ do awk things here}'

Actually, awk output is the input for myFunk(). So I am not sure if this will do the trick.

This is the awk statement ->

Code:
awk ":|/" {check 1, action 1; } /,/ {l=split($5,a,",") ; asort(a) ; myFunk() ;} {check 3, action 3;}'  filename

This is the function ->

Code:
myFunk() (
(
i=0;
while(i<l)
{
    flag=0;
    j=i+1;
    k=1;
    while[ j<l && (a[j]) eq (a[i]+1) ]do
    {
        j++;
        k++;
        flag=1;
    }
if(flag); then
{
    printf "host %s range %s %s\n", $4,a[i],a[j-1];
    i=j;
}
else
{
    printf "host %s port %s\n", $4,a[i];
}
fi
return()

The function is required to check the array values (which are already in sequence now) and will print ' line type 1 ' and if not in sequence, then print ' line type 2 .' For example; if the array has following values - 20,21,22,23,25 - then it will print this:

Code:
host <value> range 20 23
host <value> range 25

If the values are not in sequence - 20, 22, 24 - then it will print this:
Code:
host <value> range 20
host <value> range 22
host <value> range 24

Best Regards..

Last edited by morningSunshine; 03-31-2010 at 11:05 AM..
# 5  
Old 03-31-2010
Code:
s2.sh: line 48: syntax error: unexpected end of file

That's a sh script error, not an AWK error.

Quote:
Originally Posted by morningSunshine
Code:
awk ":|/" {check 1, action 1; } /,/ {l=split($5,a,",") ; asort(a) ; myFunk() ;} {check 3, action 3;}'  filename

That statement has an unmatched single quote, which is probably the reason for the sh giving an unexpected end of file error (it's looking for the end of a string when it hits end of file).

Aside from the quote, that's not a valid awk script. I would expect awk to abort with a syntax error. Is "action" a variable whose definition is omitted? Is "check" an awk user-defined funciton whose definition or inclusion you omitted? If so, you need to use parenthesis around its arguments:
Code:
{check(1, action 1)}

If "action" and "check" are not a variable and user-defined function, respectively, then I have no clue what you're going for there.

Regarding myFunk(), I really hope that's csh. Otherwise, that sh/bash/ksh script needs a lot of work Smilie

Regards,
Alister

Last edited by alister; 03-31-2010 at 11:47 AM..
# 6  
Old 03-31-2010
Quote:
Originally Posted by alister
Code:
s2.sh: line 48: syntax error: unexpected end of file

That's a sh script error, not an AWK error.

That statement has an unmatched single quote, which is probably the reason for the sh giving an unexpected end of file error (it's looking for the end of a string when it hits end of file).

Aside from the quote, that's not a valid awk script. I would expect awk to abort with a syntax error. Is "action" a variable whose definition is omitted? Is "check" an awk user-defined funciton whose definition or inclusion you omitted? If so, you need to use parenthesis around its arguments:
Code:
{check(1, action 1)}

If "action" and "check" are not a variable and user-defined function, respectively, then I have no clue what you're going for there.

Regarding myFunk(), I really hope that's csh. Otherwise, that sh/bash/ksh script needs a lot of work Smilie

Regards,
Alister
Hi Alister, sorry I was not clear about my earlier post. (check1, action1) were a sample text instead of the original script.

Here is the original script and function:

Code:
#!/bin/bash

awk -F ":|/" '/\*$/ {printf "host %s\n",$4;next}
              /,/ {l=split($5,a,",") ; asort(a) ; heybaby() ;next}
              /-/ {sub(/-/," ",$5) ; printf "host %s range %s\n",$4,$5;next}
              {printf "host %s port %s\n",$4,$5}' c2.txt

heybaby()
{
i=0;
while(i<l) do
{
    flag=0;
    j=i+1;
    k=1;
    while[ j<l && (a[j]) eq (a[i]+1) ]do
    {
        j++;
        k++;
        flag=1;
    }
    if(flag); then
    {
        printf "host %s range %s %s\n", $4,a[i],a[j-1];
        i=j;
    }
    else
    {
        printf "host %s port %s\n", $4,a[i];
    }
    fi
#        break;
} done
}

Update:
I have now been able to correct the syntax error from the script. Now upon executing the script, this error is appearing:
Quote:
awk: cmd. line:1: (FILENAME=c2.txt FNR=1) fatal: function `heybaby' not defined


Best Regards.

Last edited by morningSunshine; 03-31-2010 at 12:26 PM.. Reason: Updated the script / Syntax errors resolved
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help to Modify File Name in each function before calling another function.

I have a script which does gunzip, zip and untar. Input to the script is file name and file directory (where file is located) I am reading the input parameters as follows: FILENAME=$1 FILEDIR=$2 I have created 3 functions that are as follows: 1) gunzip file 2) unzip file... (2 Replies)
Discussion started by: pinnacle
2 Replies

2. Shell Programming and Scripting

Ceil not working as function in awk statement

Hi, I have the following code in which i am trying to find ceil of 10th & 11th fields. For finding ceil i have a function in the awk statement. When i test it for some values say on command line it gives correct response(say $10=0 & $11=750). But when the same value occurs in a file having more 3... (5 Replies)
Discussion started by: siramitsharma
5 Replies

3. Shell Programming and Scripting

Function is calling only once

In my prog if i enter the input for the 1st time it is executing correctly, but for the second time entire script is not executing it just exiting my code is #!/bin/sh checkpo() { echo "Checking the entered PO to create output text file "; IFS=$'\n' set -f var=0 for i in $(cat... (3 Replies)
Discussion started by: Padmanabhan
3 Replies

4. Shell Programming and Scripting

Calling two function

Hi, I need to run start_load function for two tables. Step 1: if HMAX_TBL_ID and GMAX_TBLI_D are same for tab_name1 then echo message "all table ids are processed" Step 2: go back and call start_load for tab_name2 and check if table id are same for table 2 too. Please let me know how to... (5 Replies)
Discussion started by: sandy162
5 Replies

5. Shell Programming and Scripting

Reading function calling statement from a file

Hi, I have a shell script 'sample.sh' which has some functions as below. #fun1 fun1() { date } #fun2() { echo hi } I want to run these functions as background processes and also redirect the output to a file. My function calling statements are in a different file 'sample.cfg' as... (3 Replies)
Discussion started by: rama_kodam
3 Replies

6. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

7. Shell Programming and Scripting

Calling Function Problem

Hi, I had a scripts which calls two function. One function will call another function, script is working fine but the second function is not calling the first function. Below is the script #!/usr/bin/ksh fun1() { echo $DATETIME >> Test1.ksh return 0 } fun2() { typeset DATETIME=`date... (5 Replies)
Discussion started by: somu_june
5 Replies

8. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

9. Shell Programming and Scripting

calling function inside awk

Hi All, My 1.txt contains some functions fun1() .... .... fun2() .... .... I can call these fun from 2.txt inside awk as below value="`fun1 "argument1"`" awk 'BEGIN {printf ("%s", "'"$value"'")}' I need to modify the above code so that without using the variable to store... (2 Replies)
Discussion started by: jisha
2 Replies

10. UNIX for Dummies Questions & Answers

Calling a function

I have created a file generic.func and it has lots of functions. One of the functions is this: Check_backup_size() { dsmc q b $BACKUP_DIR/"*.Z" | awk '{print $1}'|sed 's///g' > outputfile X=`awk '{sum += $1} END {print sum }' outputfile'` echo "$X" ls -ltr $BACKUP_DIR/"*.Z" | awk... (5 Replies)
Discussion started by: ashika
5 Replies
Login or Register to Ask a Question