PAssing variables to awk arithmetic


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PAssing variables to awk arithmetic
# 1  
Old 05-16-2013
PAssing variables to awk arithmetic

Hi all,

I am wanting to pass variables from a file to an awk arithmetic formula.
When I use the formula with the value it works well. As soon as I make these variables I get an inf (infinity) response. I can certainly echo the variables back and they look correct. My googling for answers has failed. Any ideas ???
The variables being 89,63 in this case ...
Code:
var1=`cat cre|awk -F"," '{print $1}'`
var2=`cat cre|awk -F"," '{print $2}'`
echo $var1 > ggh
echo $var2 >> ggh
cat cre|awk -F"," 'BEGIN {
print 141*($var1*0.0113/0.9)^-1.209*0.993^$var2}' >> ggh

This produces the file ggh ... giving ...
Code:
89
63
inf
~

Whereas on the command line I get ...
Code:
(Test:Working)$ awk -F"," 'BEGIN {
> print 141*(89*0.0113/0.9)^-1.209*(0.993)^63}'
79.1984

Cheers, g
# 2  
Old 05-16-2013
use -v option of awk to pass shell variables..

Code:
awk -F"," '{print $1" "$2}' cre|read var1 var2
echo $var1"\n"$var2 > ggh
awk -v v1=$var1 -v v2=$var2 'BEGIN {print 141*(v1*0.0113/0.9)^-1.209*0.993^v2}' cre >> ggh

looking at your req i dont think you have to get those values into some variable unless you wanna use them further in script.
This User Gave Thanks to vidyadhar85 For This Post:
# 3  
Old 05-16-2013
Thank you for that. Yes I am going to be feeding in a script with thousands of lines so will need the variables. I appreciate your answer thanks.
# 4  
Old 05-18-2013
Note that if you use a Korn shell newer than the 1988 version, you can do this entirely in the shell. Just note that in awk the exponentiation operator is ^ but in ksh arithmetic expansions it is **. The script:
Code:
#!/bin/ksh
IFS=, read var1 var2 < cre
printf "%s\n%s\n%.4f\n" "$var1" "$var2" \
        "$((141*(var1*0.0113/0.9)**-1.209*(0.993)**var2))" > ggh

when the file cre contains:
Code:
89,63

as its first line places the output:
Code:
89
63
79.1984

in the file ggh.

If cre contains thousands of lines like the above, this could easily be converted to a while read loop to process all of the lines in the file:
Code:
#!/bin/ksh
while IFS=, read var1 var2
do      printf "%s\n%s\n%.4f\n" "$var1" "$var2" \
                "$((141*(var1*0.0113/0.9)**-1.209*(0.993)**var2))"
done < cre > ggh

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - passing variables in and out

Am looking to pass some Linux environment variables into AWK , can I simply use the -v option ? awk -F: -v AHOME=$HOME '{ if {rm AHOME/file.txt a=2 } }' config.txt ... (4 Replies)
Discussion started by: alldbest
4 Replies

2. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

3. Shell Programming and Scripting

Passing variables to awk

Hi guys, I need to fetch data from logfile between two given dates,i got the below code from our forum.It works perfect,but i need to enter the value dynamically to awk while running. awk '/2012 Jun/{p=1}!/2012 Jul/ && prev~/2012 Jul/ && p{p=0}{prev=$0}p' file i tried the below code,but... (4 Replies)
Discussion started by: mohanalakshmi
4 Replies

4. Shell Programming and Scripting

Passing variables into AWK

I'm trying to use awk to write new entries to a hosts file if they don't exist. I need to do so depending on the type of system I have. Below is what I have, but it isn't working. awk -v myip1=$IP1 myip2=$IP2 myhost1=$HOST1 myhost2=$HOST2' BEGIN { mqhost1=0; mqhost2=0; stap1=0; stap2=0; } ... (4 Replies)
Discussion started by: Boomn4x4
4 Replies

5. Shell Programming and Scripting

ksh passing to awk multiple dyanamic variables awk -v

Using ksh to call a function which has awk script embedded. It parses a long two element list file, filled with text numbers (I want column 2, beginning no sooner than line 45, that's the only known thing) . It's unknown where to start or end the data collection, dynamic variables will be used. ... (1 Reply)
Discussion started by: highnthemnts
1 Replies

6. Shell Programming and Scripting

Passing awk variables to shell

Hi. I need to parse file and assign some values to variables, right now i do like below MYHOMEDIR=`awk '/Home/ {print $NF}' output.txt` MYSHELL=`awk '/Shell/ {print $NF}' output.txt` PRGRP=`awk '/Primary/ {print $NF}' output.txt` SECGRP=`awk '/Second/ {print $NF}' output.txt` In this... (10 Replies)
Discussion started by: urello
10 Replies

7. UNIX for Dummies Questions & Answers

Passing Shell Variables to an awk command

Hello, I have two files File1 & File2. File1 76 135 136 200 250 345 .... File2 1 24 1 35 1 36 1 72 .... I want to get all the values form File2 corresponding to the range in File 1 and feed it to a program. Is the code below right? Can I pass shell variables to awk in this... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

8. Shell Programming and Scripting

Passing awk Variables

I am trying to pass the results from a variable gathered from awk, however when I echo the 'PARSE' and 'SUB', the response is blank. This is my command. awk -F= '/Unit/''{ PARSE=substr($2,1,5) ; SUB=substr($2,1,1) }' inputfile.lst Is this a kind of valid attempt or am I obligated to declare... (3 Replies)
Discussion started by: gozer13
3 Replies

9. Shell Programming and Scripting

Passing Variables to Awk

Hi I have a unix shell script with an awk statement. I would like to print some of the fields of an input file. However, I would like to print them dynamically, ie by passing the literal $1 $3 into the script to define the output. I have tried the following: variable1='$1' awk... (2 Replies)
Discussion started by: Bab00shka
2 Replies

10. Shell Programming and Scripting

Passing Variables to AWK

Does anybody have an explanation for the following: The following scripts runs fine on IRIX64 6.5 but has bugs on Solaris 8. #! /bin/sh echo run only on an SGI machine echo type in linenumber read j echo value read value awk -f rmspass2 level=$value $j'step1.mlf' When the script is... (5 Replies)
Discussion started by: AreaMan
5 Replies
Login or Register to Ask a Question