If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > decimal point ....

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-13-03, 03:20
jinroh jinroh is offline
Registered User
 
Join Date: Oct 2003
Posts: 14
Arrow decimal point ....

expr 5 / 2

the out is 2, how do i get the output to display the decimal point ?

i tried awk with printf but can't seem to get the correct command do help plz
Reply With Quote
  #2 (permalink)  
Old 10-13-03, 04:26
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
'expr' can only perform integer arithmetic. There are probably more suitable utilities but seen as I tend to use awk for everything...

echo 5 2 | awk '{print $1/$2}'

HTH
Reply With Quote
  #3 (permalink)  
Old 10-13-03, 05:15
jinroh jinroh is offline
Registered User
 
Join Date: Oct 2003
Posts: 14
thanx guru, but what if i want to divide using variable ?

eg i=5
y=2

how do i use $i/$y ? i tried awk '{print $i/$y}' doesn't seem to work .. can give further guidance guru ?
Reply With Quote
  #4 (permalink)  
Old 10-13-03, 05:17
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Like this?

echo $i $y | awk '{print $1/$2}'
Reply With Quote
  #5 (permalink)  
Old 10-13-03, 05:21
jinroh jinroh is offline
Registered User
 
Join Date: Oct 2003
Posts: 14
must i pass the variable into echo ? ... is there another way ? do you know what does awk 'printf' does ? i remember someone told me this but dont understand it
Reply With Quote
  #6 (permalink)  
Old 10-13-03, 05:37
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
No, you needn't pass your variable in using echo. The thing is, awk must take an input from somewhere, so why not use echo? Awk doesn't have to work with, or output what you input but it does need something (otherwise it will expect you to enter input at the command line). You can get round this by doing your processing in a BEGIN block which includes an exit...

awk -v i=5 -v y=2 'BEGIN {print i/y; exit}'

The -v flag here is declaring variables to be used in the awk processing.

HTH
Reply With Quote
  #7 (permalink)  
Old 10-13-03, 05:41
jinroh jinroh is offline
Registered User
 
Join Date: Oct 2003
Posts: 14
thanx guru

i found another way using bc

eg echo "scale=2; 5/2" | bc -l output 2.50 cause i put the scale 2

Last edited by jinroh; 10-13-03 at 08:47.
Reply With Quote
  #8 (permalink)  
Old 10-13-03, 14:51
fla5do fla5do is offline
Registered User
 
Join Date: Oct 2003
Location: Germany
Posts: 138
Hello jinroh,
what does the "eg" means in your posting ???

For further operations whith the result from awk in your script try this
i=5; y=2
echo $i
echo $y
x=`echo $i $y | awk '{
print $1/$2
}'`
echo $x

Output to screen is "2,50"

@Damian, thanks for your solutions. I´m very avid.

Greetings from Germany
Peter F.

Last edited by fla5do; 10-13-03 at 15:00.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On