0

Sample function:

$ testfn() { echo "${1} world"; }
$ testfn hello
hello world

awk example:

$ echo "something bla bla2"|awk '$1 ~/something/ { print $0; }'
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:

$ echo "something bla bla2"|awk '$1 ~/something/ { $1="'"$(testfn) $1"'" ; print $0 }'
 world  bla bla2

^^ Above doesn't work

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

2
  • The awk example in the 2nd block only prints "something bla bla2", it doesn't print the first line of "something". Is that a typo or is it something else? What OS is this and what version of awk/gawk are you using? Commented Feb 10, 2013 at 6:45
  • typo, corrected it. it's RHEL5. Commented Feb 10, 2013 at 18:44

2 Answers 2

2

Highly unrecommended but:

$ cat .env
testfn() { echo "${1} world"; }
$ echo $BASH_ENV
.env
$ echo "something bla bla2" | awk '$1 ~/something/{"bash -c \"testfn " $1 "\"" | getline $1; print $0}'
something world bla bla2

Now, tell us what you're really trying to do and we can help you write a sane script to do that.

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

2 Comments

Did everything as you've described. But getting errors: BASH_ENV=.env $ echo "something bla bla2" | awk '$1 ~/something/{"bash -c \"testfn " $1 "\"" | getline $1; print $0}' sh: -c: line 0: unexpected EOF while looking for matching `"' sh: -c: line 1: syntax error: unexpected end of file something bla bla2 I'm sending a table of values to awk. What I want is to replace one field in a 5th column, based on element in 1st column, and this element should be processed via complex function (based on SQL query result from database).
Maybe your shell isn't bash and/or bash isn't available on your box? Just use whatever shell you normally use and save your function in whatever file your shell executes when a new non-login shell is started. You shouldn't do this anyway though. Unfortunately your explanation doesn't contain nearly enough information to help you write a script. If you'd like help, edit your question with sample input/output and a description of what your function should do.
1

No - you can't call shell functions from inside of awk. It's not a shell. However, a common workaround involves adding your function to a file and calling it using awk's system() function. Here's a simple example:

Contents of yourfunction.sh:

{
    echo "$1 world"
}

Then run:

echo "what the ..." | awk '{ system("./yourfunction.sh" FS $1) }'

Results:

what world

Note that if you find yourself having to do this, there's almost certainly a better approach. What are you actually trying to do here?


EDIT:

In response to the comments below, use the getline() function:

echo "what the ..." | awk '{ "./yourfunction.sh" FS $1 | getline $1 }1'

Results:

what world the ...

Here's some info re the getline() function:

http://www.gnu.org/software/gawk/manual/html_node/Getline.html

3 Comments

Thank you! If I will not find another ways to achieve what I want, I'll have to try this workaround.
Hey, but this executes the function and displaying the output, what I'm trying to achieve is to replace fist element and then print the whole modified line. $ echo "what the ..." | awk '{ $1=system("./yourfunction.sh" FS $1); print; }' what world 0 the ...; I'm expecting result here as one line: "what world the ..."
If you're considering using getline, make sure you read awk.info/?tip/getline and fully understand what you're getting yourself into.

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.