awk - Why can't value of awk variables be passed to external functions ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - Why can't value of awk variables be passed to external functions ?
# 15  
Old 11-25-2014
Quote:
Originally Posted by sreyan32
I am trying to get a concrete idea on what awk can and cannot do by pushing its less used and documented features to the limit.
This is not an awk feature, just a bash feature.

Also, you're not really "calling" bash functions. You're generating an entire new shell which calls the functions. This means you can't use the function to set shell variables or anything -- they will vanish when this new shell quits.
# 16  
Old 11-29-2014
Quote:
Originally Posted by Corona688
You messed up te same way in many places. Remember awk is not shell -- $k means column k. Stop using $k when you don't want column. Start using k.
Sir, I have tried all possible combinations of $k and k. And neither of them give me my expected output. Either I am not able grasp what you are telling me or you don't understand what I want. Or maybe it can't be done all together.

I posting various combinations of scripts that I have tried with their respective outputs-:

First Sample Script-: (Where all $k has been replaced with k)

Code:
 
 function my_local_function () {
 echo $1;
 }
 export -f my_local_function
 echo $1 | awk -F"/" '{ for (k=1;k<=NF;k++) { if ( k == "a" ) { system("my_local_function" k) } else { print k } } }'

Output-:
Code:
1
2
3
4

As you can see above no errors but I don't get my expected output. Since I am using k and not $k. Since k represents the loop variable and not its value.

Second Sample Script-: (Where I am using system("my_local_function" k) and $k in all other places)

Code:
 
 function my_local_function () {
 echo $1;
 }
 export -f my_local_function
 echo $1 | awk -F"/" '{ for (k=1;k<=NF;k++) { if ( $k == "a" ) { system("my_local_function" k) } else { print $k } } }'

Output-:
Code:
sh: my_local_function1: command not found
b
c
d

Third Sample script: (Where I am using system("my_local_function k") and $k in all other places)

Code:
 
 function my_local_function () {
 echo $1;
 }
 export -f my_local_function
 echo $1 | awk -F"/" '{ for (k=1;k<=NF;k++) { if ( $k == "a" ) { system("my_local_function k") } else { print $k } } }'

Output-:
Code:
k
b
c
d

Again not a correct output but error free.

Fourth Sample Script-: (Where I am using system("my_local_function $k") and $k in all other places)

Code:
 
 function my_local_function () {
 echo $1;
 }
 export -f my_local_function
 echo $1 | awk -F"/" '{ for (k=1;k<=NF;k++) { if ( $k == "a" ) { system("my_local_function $k") } else { print $k } } }'

Output-:

Code:

b
c
d

One blank line and the rest of the output is OK.

So basically what I want to do is call a BASH function from awk and pass the value of an awk variable as an argument. CAN IT BE DONE ?

And please can you post an entire working script next time rather than telling me what to do. It will avoid any further confusions.
# 17  
Old 11-29-2014
Your "sample two script" had two typos in the systemcall, a space before the trailing quote of "my_local_function" and a missing '$' before the 'k':
Code:
function my_local_function () { echo @ $1; }
export -f my_local_function
echo $1 | awk -F"/" '{ for (k=1;k<=NF;k++) { if ($k == "a") { system("my_local_function " $k) } else { print $k } } }'

I added a '@' to the local function just for clarity:
Code:
$ bash x a/b/c/d/e
@ a
b
c
d
e

Is this what was desired?

Please note that you'll be creating a subshell everytime you want to invoke the local function. An alternative to consider:
Code:
function my_local_function () { echo @ $1; }
export -f my_local_function
echo $1 \
| awk -F"/" '{ for (k=1;k<=NF;k++) print ( $k == "a" ? "my_local_function" : "echo" ), $k; }' \
| while read line; do
    eval $line
done

This User Gave Thanks to derekludwig For This Post:
# 18  
Old 11-29-2014
Quote:
Originally Posted by sreyan32
---------- Post updated at 11:30 PM ---------- Previous update was at 11:26 PM ----------

Quote:
Originally Posted by sea
As i see it, not really understanding awk yet, but you're comparing a STRING (a) with an INTEGER (k).
Anyhow, so you pass a list of variables, and when it matches a certain string, you want to call that function?

Must it be awk, because bash works just fine Smilie
Code:
#!/bin/bash
my_func_local() {
    echo "In func: my_func_local $1"
}

for ARG in "${@}";do
    [[ $ARG = a ]] && \
        my_func_local $ARG || \
        echo $ARG
done

Code:
sh script.sh a b c d
In func: my_func_local a
b
c
d

Hope this helps
Can you please explain to me what your script means step by step ? Sorry for being blunt but I am comfortable with awk rather than raw shell scripting. However for this problem it does not matter what I use so if you can explain how your script works then it would be fine for my purpose.
Sure:
Code:
#!/bin/bash
# Definition of a function, as you already done
my_func_local() {
    echo "In func: my_func_local $1"
}

# Regular parsing through all passed arguments
# This reads one passed argument after another into the variable ARG
for ARG in "${@}";do
    # Compare current ARG-ument with letter 'a', 
    #   IF SO (&&), call 'my_func_local a', 
    #   otherwise (||) just print current ARG
    [[ $ARG = a ]] && \
        my_func_local $ARG || \
        echo $ARG
done

Hope this helps
This User Gave Thanks to sea For This Post:
# 19  
Old 11-29-2014
Sea,

The for loop need to be:
Code:
IFS="${IFS}/"
for ARG in ${@}; do

so the arguments can be separated by the field separator "/".
This User Gave Thanks to derekludwig For This Post:
# 20  
Old 12-01-2014
And, because of unquoted "$@", it is safer to do
Code:
set -f # disable globbing on wildcards

and nevertheless quote "$ARG", and use if-then-else because && - || is harder to read and a non-zero exit code in the && branch will continue with the || branch.
Code:
#!/bin/bash
# Definition of a function, as you already done
my_func_local() {
    echo "In func: my_func_local $1"
}

# Regular parsing through all passed arguments
# This reads one passed argument after another into the variable ARG

# By putting the following in a ( subshell ) we limit the scope of the next two statements to the subshell
(
set -f # disable globbing on wildcards
IFS="${IFS}/"
for ARG in $@ ;do
    # Compare current ARG-ument with letter a
    if [[ "$ARG" = "a" ]]; then
        my_func_local "$ARG"
    else
        echo "$ARG"
    fi
done
)
# subshell ended, IFS and settings restored.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Variables passed to awk to return certain rows

Hi Forum. I have the following test.txt file and need to extract certain rows based on "starting position", "length of string" and "string to search for": 1a2b3d 2a3c4d ..... My script accepts 3 parameters: (starting col pos, length to search for, string to search for) and would like to... (4 Replies)
Discussion started by: pchang
4 Replies

2. Shell Programming and Scripting

awk processing of passed variables

Currently have this: set current=192.168.0.5 set servicehost = `echo $current | awk -F. '{print $4}'` echo $numberoffields 5 ..but would like to reduce # of variables and eliminate echo to have something like this: set servicehost = `awk -v s="$current" -F. 'BEGIN{print $2}'`But... (3 Replies)
Discussion started by: Mid Ocean
3 Replies

3. Shell Programming and Scripting

ksh passing to awk multiple dyanamic variables awk -v

Using ksh to call a function which has awk script embedded. It parses a long two element list file, filled with text numbers (I want column 2, beginning no sooner than line 45, that's the only known thing) . It's unknown where to start or end the data collection, dynamic variables will be used. ... (1 Reply)
Discussion started by: highnthemnts
1 Replies

4. Shell Programming and Scripting

Assign value to external variables from awk

Hello I have a text file with the next pattern Name,Year,Grade1,Grade2,Grade3 Name,Year,Grade1,Grade2,Grade3 Name,Year,Grade1,Grade2,Grade3 I want to assign to external variables the grades using the awk method. After i read the file line by line in order to get the grades i use this ... (2 Replies)
Discussion started by: Spleshmen
2 Replies

5. Shell Programming and Scripting

arithmetic from csh variable passed to awk

I have the following code in a csh script I want to pass the value of the variable sigmasq to the awk script so that I can divide $0 by the value of sigmasq grep "Rms Value" $f.log \ | awk '{ sub(/*:*\.*/,x); \ print... (2 Replies)
Discussion started by: kristinu
2 Replies

6. Shell Programming and Scripting

flags passed to an awk script

I have an awk script script.awk for example and want to pass a flag (let's call it "neat") so that the data is put into nice columns. For example like this awk -v neat -f script.awk fin > fout Then check inside the program if the use has put neat, if yes I output the lines in nice columns,... (1 Reply)
Discussion started by: kristinu
1 Replies

7. Shell Programming and Scripting

AWK: Retrieving names of variables passed with -v

I'm an experienced awk user, but this one has me stumped. I have an awk script which is called from a UNIX command line as you'd expect: myscript.awk -v foo=$1 -v bar=$2 filename My question is this: is there a mechanism for determining the names of the -v variables within a script? ... (3 Replies)
Discussion started by: John Mac
3 Replies

8. Shell Programming and Scripting

awk - arithemetic functions with external variables

I'm trying to get awk to do arithmetic functions with external variables and I'm getting an error that I cannot figure out how to fix. Insight would be appreciated money=$1 rate1=$(awk -F"\t " '/'$converting'/{print $3}' convert.table) rate2=$(awk -F"\t"... (2 Replies)
Discussion started by: DKNUCKLES
2 Replies

9. UNIX for Dummies Questions & Answers

variable passed to awk

Does anybody know how to print a variable passed to awk command? awk -F"|" 'BEGIN {print $job,"\n","Question \n"} {print $1,$2$4," ",$3}' "job=$job1" file1 I am trying to pass job the variable job1. the output is blank. ?? (3 Replies)
Discussion started by: whatisthis
3 Replies

10. UNIX for Dummies Questions & Answers

variable passed to awk

Anybody know what's wrong with this syntax? awk -v job="$job" 'BEGIN { FS="|"} {print $1,$2," ",$4," ",$3\n,$5,"\n"}' list It's keeping give me this message: awk: syntax error near line 1 awk: bailing out near line 1 It seems awk has problem with my BEGIN command. Any... (8 Replies)
Discussion started by: whatisthis
8 Replies
Login or Register to Ask a Question