Find the answer to your Linux question:
Results 1 to 3 of 3
awk: How pass parameters to the function, called from inside awk ? Hello, Experts! Please, help me to figure this out: Here is sample function: Code: $ testfn() { echo ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2013
    Posts
    2

    Question awk: How pass parameters to the function, called from inside awk ?


    awk: How pass parameters to the function, called from inside awk ?

    Hello, Experts!

    Please, help me to figure this out:

    Here is sample function:
    Code:
    $ testfn() { echo "${1} world"; }
    $ testfn hello
    hello world
    awk example:
    Code:
    $ echo "something bla bla2"|awk '$1 ~/something/ { print $1; print $0; }'
    something
    something bla bla2

    Now, I want to change "something" to "something world" using created above function, when printing it as a whole line, by passing first awk "column element" as a parameter:
    Code:
    $ echo "something bla bla2"|awk '$1 ~/something/ { $1="'"$(testfn) $1"'" ; print $0 }'
     world  bla bla2
    ^^Above doesn't work^^
    Code:
    $ echo "something bla bla2"|awk '$1 ~/something/ { $1="'"$(testfn)"$1'" ; print $0 }'
     world bla bla2
    ^^Above doesn't work^^
    Code:
    $ echo "something bla bla2"|awk '$1 ~/something/ { $1="'"$(testfn)"'$1" ; print $0 }'
     world$1 bla bla2
    ^^Above doesn't work^^

    Is thee any way to pass parameters from awk to the function inside awk ?


    Thanks!

  2. #2
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    hi,

    why don't you declare the function inside awk ?
    Code:
    awk 'function sayHello(arg)
    {
    print "Hello "arg
    }
    /^something$/{sayHello($1)}'

  3. #3
    Just Joined!
    Join Date
    Feb 2013
    Posts
    2
    Thanks! And would it be possible to call shell function from declared function inside awk ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •