The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problem in arithemetic operation subin_bala Shell Programming and Scripting 3 04-23-2008 11:47 AM
Use of functions amitrajvarma Shell Programming and Scripting 1 11-23-2007 06:52 AM
How to parse config variables from external file to shell script pradsh Shell Programming and Scripting 2 07-09-2007 03:21 PM
Variables and functions dfb500 Shell Programming and Scripting 4 05-21-2007 05:23 PM
arithemetic problem anujairaj UNIX for Dummies Questions & Answers 9 05-01-2006 12:55 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-30-2007
DKNUCKLES DKNUCKLES is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 12
awk - arithemetic functions with external variables

I'm trying to get awk to do arithmetic functions with external variables and I'm getting an error that I cannot figure out how to fix.

Insight would be appreciated


Code:
        money=$1
        rate1=$(awk -F"\t " '/'$converting'/{print $3}' convert.table)
        rate2=$(awk -F"\t" '/'$converted'/{print $3}' convert.table)
        awk -F"\t" '{printf '$rate2'/'$rate1'*'$money'}'

OUTPUT

awk: cmd. line:1: {printf 3.3513
awk: cmd. line:1: ^ unexpected newline or end of string
  #2 (permalink)  
Old 12-01-2007
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,941
You cannot pass external variables into awk in the manner that you are attempting to use.

Try:


Code:
awk -F"\t" -v rate1=$rate1 -v rate2=$rate2 -v money=$money '{print rate2/rate1*money}'

  #3 (permalink)  
Old 12-01-2007
prowla prowla is offline
Read Only
  
 

Join Date: Nov 2007
Posts: 165
I'm not at a UNIX machine now, but this should work:


Code:
        money=$1
        rate1=$(awk -F"\t " '/'$converting'/{print $3}' convert.table)
        rate2=$(awk -F"\t" '/'$converted'/{print $3}' convert.table)
        awk -F"\t" '{printf r2/r1*m}' r2=$rate2 r1=$rate1 m=$money

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:13 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0