Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

Try to use system() function:

awk '{printf("%s ",$1); system("d2h " $2)}' file

In your case system will call d2h 247808 and then append output of this command to printf output:

Mike 3C800

EDIT:

As system uses sh instead of bash I can't find a way to access .bashrc. But you can still use functions from your current bash script:

#!/bin/bash
d2h() {
    # do some cool conversion here
    echo "$1" # or just output the first parameter
}
export -f d2h
awk '{printf("%s ",$1); system("bash -c '\''d2h "$2"'\''")}' file

EDIT 2:

I don't know why, but this is not working on my Ubuntu 16.04. This is strange, because it used to work on Ubuntu 14.04.

share|improve this answer
    
This could work if d2h were an executable, but not if it's a "function defined either in .bashrc or in shell script". – Michael Homer Sep 22 '14 at 8:33
    
@MichaelHomer yes, you are right. Thanks to your comment I realised I have answered the question nobody asked. But I have found a way to use functions from the current script (and possibly from other scripts via source) but I can'f figure out why it doesn't work with .bashrc. – akarilimano Sep 22 '14 at 10:10

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

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

Using a user defined bash function inside awk

Disclaimer: I realize this is not what the OP is trying to do, but Google will lead others like me to this answer.

Situation

You have a bash script is organized with functions (because you do not hate yourself or [most] coworkers) and at least 1 of those functions needs to call another from within awk.

Solution

Script

#!/bin/env bash

# The main function - it's a sound pattern even in BASH
main(){
    # In the awk command I do some tricky things with single quotes. Count carefully...
    # The first $0 is outside the single quotes so it is the name of the current bash script.
    # The second $0 is inside the single quotes so it is awk's current line of input.
    awk '{printf("%s. ", ++c); system("'$0' --do"); print $0}'<<-PRETEND_THIS_IS_AN_INPUT_STREAM
        and
        and
        well
    PRETEND_THIS_IS_AN_INPUT_STREAM
}

# functionized to keep things DRY
doit(){
    echo -n "doin' it "
}


# check for a command switch and call different functionality if it is found
if [[ $# -eq 1 && $1 == "--do" ]];
then
        doit
else
        main
fi

Output

$ ./example.sh
1. doin' it and
2. doin' it and
3. doin' it well
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.