Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is it possible to use bash function inside AWK somehow?

Example file (string, int, int, int)

Mike 247808 247809 247810

Trying to convert values from decimal to hexadecimal.

Function defined either in .bashrc or in shell script.

awk '{print $1 ; d2h($2)}' file

awk: calling undefined function d2h input record number 1, file file source line number 1

share|improve this question

2 Answers

You can call bash from awk and use its output. That is obviously dangerous from a performance perspective if it happens too often. Quoting the man page:

command | getline [var]

Run command piping the output either into $0 or var,

command would be a bash script that contains the function definition and executes the function.

share|improve this answer

Try doing this :

awk '{print $1 ; printf "%x\n", $2}' file

AFAIK, you can't use a bash function in awk but only a script. You can use an awk function if needed.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.