How to call a local function within Awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to call a local function within Awk
# 1  
Old 08-18-2008
Tools How to call a local function within Awk

Hi,
I have the following statement which parses a string for me and prints it out:
l_comp="dc000.runksh.test.ksh|
$g_sql/dc0000.runksh_test.sql|new.dat|control.ctl"

echo $l_comp | awk -F"|" '{ for ( i = 1; i <= NF; i++) { print $i; } } '

Rather then printing the data, I would like to pass the value to a local function like this, but it is not working.

echo $l_comp | awk -F"|" '{ for ( i = 1; i <= NF; i++) { lf_verify_component $i; } } '

Can someone point to what I am doing wrong and how I can correct?
# 2  
Old 08-18-2008
You can't call shell functions from awk, or vice versa.

Perhaps you mean

Code:
for i in $l_comp; do
  If_verify_component "$i"
done

It's not entirely clear what the problem is, so if this doesn't help, perhaps you can describe what If_verify_component is.
# 3  
Old 08-18-2008
l_comp is a string of pipe delimited file names, I want to parse the string and then verify that the file exist where I expect it to exist.

lf_verify_component () {
l_comp_file=$i
l_comp_suffix=`echo "$l_comp_file" | awk -F'.' {'print $2'}`
l_valid_comp="N"

# Does the file exist?
if [[ ! -f $l_comp_file ]] && [[ ! -f $path/$l_comp_file ]];then
if [[ `echo $l_comp_suffix` = "ksh" ]] || [[ `echo $l_comp_suffix` = "KSH" ]] || [[ `echo $l_comp_suffix` = "" ]];
then
if [[ -f $g_bin/$l_comp_file ]];then
l_valid_comp="Y"
fi
elif [[ `echo $l_comp_suffix` = "ctl" ]] || [[ `echo $l_comp_suffix` = "CTL" ]] || [[ `echo $l_comp_suffix` = "sql
" ]] || [[ `echo $l_comp_suffix` = "SQL" ]] || [[ `echo $l_comp_suffix` = "dat" ]] || [[ `echo $l_comp_suffix` = "DAT
" ]];then
if [[ -f $g_sql/$l_comp_file ]];then
l_valid_comp="Y"
fi
fi
else
l_valid_comp="Y"
fi

if [[ $l_valid_comp = "N" ]];then
echo 'ERROR VALIDATING SOURCE FOR THE COMPONENT FILE: ' $l_comp_file >> $l_spool_file
exit 1
fi
# 4  
Old 08-18-2008
That's a massive amount of echo in backticks. Is there a reason the input is pipe delimited? It would be less obscure to handle if it were whitespace-delimited.

Code:
OLDIFS=$IFS
IFS='|'
for i in $l_comp; do
  If_verify_component "$i"
done
IFS=$OLDIFS

If_verify_component () {
  l_comp_file=$i
  l_comp_suffix=${i#*.}
  l_valid_comp="N"

  # Does the file exist?
  if [[ ! -f $l_comp_file ]] && [[ ! -f $path/$l_comp_file ]];then
    case $l_comp_suffix in [Kk][Ss][Hh]|"")
        if [[ -f $g_bin/$l_comp_file ]];then
          l_valid_comp="Y"
        fi;;
      CTL|ctl|SQL|sql|DAT|dat)
        if [[ -f $g_sql/$l_comp_file ]];then
          l_valid_comp="Y"
        fi;;
    esac
  else
    l_valid_comp="Y"
  fi

  if [[ $l_valid_comp = "N" ]];then
    echo 'ERROR VALIDATING SOURCE FOR THE COMPONENT FILE: ' $l_comp_file >> $l_spool_file
    exit 1
  fi
}

I'm not sure I got the nesting right. It would help if you used code tags when posting scripts.
# 5  
Old 08-18-2008
Thank you very much, I appreciate your help.
The problem I am still having is that I am looking to find the final extension of a file name and the file name may have multiple nodes.

If the l_comp="dc000.runksh.test.ksh|/dbmgtu01/app/oracle/orbitz/1.0.0/sql/dc0000.runksh_test.sql|new.dat|control.ctl"

lf_verify_component () {

l_comp_file=$i
l_comp_suffix=${i#*.}

echo ' Component File: ' $l_comp_file
echo ' Component Suffix: ' $l_comp_suffix

} # end of lf_verify_component

The outpiut I get is:
Component File: dc000.runksh.test.ksh
Component Suffix: runksh
Component File: /dbmgtu01/app/oracle/orbitz/1.0.0/sql/dc0000.runksh_test.sql
Component Suffix: 0
Component File: new.dat
Component Suffix: dat
Component File: control.ctl
Component Suffix: ctl

How can I code it so that it only gives me the string after the final "."?
# 6  
Old 08-18-2008
If your shell supports ${i##*.} then use that notation. Otherwise, reverting back to an awk script might be the most straightforward solution.

Code:
l_comp_suffix=`echo "$i" | awk -F. '{ print $NF }'`

By the way, the function should properly use $1 instead of the global variable $i, but that's rather academic at this point. If your real script is much larger than this, such things will start to matter.

Incidentally, your output doesn't really match what your code does. For example, the output for dc000.runksh.test.ksh should be runksh.test.ksh (however it does match what your original awk script would have produced).
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk call in bash function called with arugments not working, something lost in translation?

Hello, I have this awk code in a bash script to perform a find and replace task. This finds one unique line in a file and substitutes the found line with a replacement. #! /bin/bash # value determined elsewhere total_outputs_p1=100 # file being modified... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

2. UNIX for Beginners Questions & Answers

Call user defined function from awk

My requirement is to call function ("fun1") from awk, and print its returned value along with $0. fun1() { t=$1 printf "%02d\n", $t % 60; } echo "Hi There 23" | awk '{print $0; system(fun1 $3)}' Any suggestions what to be modified in above code to achieve requirement.. (5 Replies)
Discussion started by: JSKOBS
5 Replies

3. Shell Programming and Scripting

After exit from function it should not call other function

Below is my script that is function properly per my conditions but I am facing one problem here that is when one function fails then Iy should not check other functions but it calls the other function too So anyone can help me how could i achieve this? iNOUT i AM GIVING TO THE... (1 Reply)
Discussion started by: rohit22hamirpur
1 Replies

4. Shell Programming and Scripting

Call shell script function from awk script

hi everyone i am trying to do this bash> cat abc.sh deepak() { echo Deepak } deepak bash>./abc.sh Deepak so it is giving me write simply i created a func and it worked now i modified it like this way bash> cat abc.sh (2 Replies)
Discussion started by: aishsimplesweet
2 Replies

5. Shell Programming and Scripting

remotely call function from local script

The following code doesn't work properly which means it doesn't displays remote output. #!/bin/ksh #################### Function macAddressFinder ######################## macAddressFinder() { `ifconfig -a > ipInterfaces` `cat ipInterfaces` }... (2 Replies)
Discussion started by: presul
2 Replies

6. Shell Programming and Scripting

awk , function call problem

#!/bin/bash awk ' function ad(t,r){ return (t+r); } BEGIN{ print ad(5,3); } { print ad(5,3); } ' Doesn't print anything for the last print ad(5,3); (6 Replies)
Discussion started by: cola
6 Replies

7. Infrastructure Monitoring

diffrence between method call and function call in perl

Hello, I have a problem with package and name space. require "/Mehran/DSGateEngineLib/general.pl"; use strict; sub System_Status_Main_Service_Status_Intrusion_Prevention { my %idpstatus; my @result; &General_ReadHash("/var/dsg/idp/settings",\%idpstatus); #print... (4 Replies)
Discussion started by: Zaxon
4 Replies

8. Shell Programming and Scripting

How to execute local function in awk

Hi All, Can you please tell me how to execute local function written in a shell script with awk. i tried with system command but its giving an error. (1 Reply)
Discussion started by: krishna_gnv
1 Replies

9. Shell Programming and Scripting

function call

hi, can any one help me to correct this function call. awk -F "," '{ {first=$1; sec=$2; tro=$3;quat=$4 } if (tro == "") { $3 = search "$file2" "$first" "$file3" {print $1","$2","$3","$4} } else {print $1","$2","$3 $4}}' $file1 > $file search () { (2 Replies)
Discussion started by: kamel.seg
2 Replies

10. Shell Programming and Scripting

call function

I have a function check_ok in my abc.sh. which return me 1 or 0 . I want to call this fuction through other shell script. this shell also send two parameter to calling function. Can you please tell me how. I am very new in unix. #!/bin/bash date_equal() { sqlplus -silent... (4 Replies)
Discussion started by: Jamil Qadir
4 Replies
Login or Register to Ask a Question