Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Awk with function calling error in redirection

awk -F "|" 'function decToBin(dec) { printf "ibase=10; obase=2; $dec" | bc; } BEGIN {print $3" "$4" "$5" "$6" "$7" "decToBin($8)}' $Input

where Input is the path to file having

1|2|1.00|0.46|0.44|1.12|49.88|3
2|2|1.00|0.45|0.55|1.13|50.12|11

It was working correctly without function calling but after introducing function decToBin() it gives error. it gives error as

awk: fatal: expression for `|' redirection has null string value

got stuck dont know how to do that please need help

like image 492
Nitin Mishra Avatar asked Jan 27 '26 04:01

Nitin Mishra


1 Answers

myawkscript.awk:

function decToBin(dec) {
    cmd="echo 'ibase=10; obase=2;" dec ";'|bc";
    cmd|getline var
    return var
}

//{print $3" "$4" "$5" "$6" "$7" "decToBin($8)}

Then

gawk -F"|" -f myawkscript.awk myfile

Gives you

1.00 0.46 0.44 1.12 49.88 11
1.00 0.45 0.55 1.13 50.12 1011

as expected

like image 111
lc2817 Avatar answered Jan 28 '26 19:01

lc2817



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!