round decimal values and use in loops


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting round decimal values and use in loops
# 1  
Old 10-07-2010
round decimal values and use in loops

i have a file in which 3 values are stored like --4.72 4.42 3.86
what i wanna do is that take read each value and compare with a fixed value say 5 but cant do so as i am getting an error for the same.

please check the code

Code:
cat /home/nsadm/auto/Logging/uptime.txt| while read a b c
do
if [ $a -gt 5 ]  then
  echo "LOAD AVG is high in MUM for --Command--upime" 
else
echo "LOAD AVG is normal in MUM"
fi 
done

Please help how can i round the value and compare hrough the loops
Thanxx in advance

Regards

G.C

Last edited by Franklin52; 10-07-2010 at 03:39 AM.. Reason: Please use code tags, thank you!
# 2  
Old 10-07-2010
You can round a value with something like:
Code:
value=$(echo $a | awk '{printf("%d\n",$0+=$0<0?-0.5:0.5)}')

# 3  
Old 10-07-2010
Better to avoid cat to read a file.
For the integer part you can use this ${a%.*}
So your script becomes
bash code:
  1. while read a b c
  2. do
  3.    if &#91; ${a%.*} -gt 5 ]
  4.    then   echo "LOAD AVG is high in MUM for --Command--upime"
  5.    else   echo "LOAD AVG is normal in MUM"
  6.    fi
  7. done </home/nsadm/auto/Logging/uptime.txt
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Round values only when it's numerics

Hi, all I have a field in a file looks like this(hundreds of lines): inf 1.24101 -0.185947 -0.349179 inf 0.126597 0.240142 -0.12031And what I expect is: inf 1.241 -0.186 -0.349 inf 0.127 (7 Replies)
Discussion started by: nengcheng
7 Replies

2. Shell Programming and Scripting

Decimal field round of

Hi, I want to round of decimal numbers in comma seperated lines till 2 decimal numbers. line will be like. ... (4 Replies)
Discussion started by: vishal0746
4 Replies

3. UNIX for Beginners Questions & Answers

Decimal numbers and letters in the same collums: round numbers

Hi! I found and then adapt the code for my pipeline... awk -F"," -vOFS="," '{printf "%0.2f %0.f\n",$2,$4}' xxx > yyy I add -F"," -vOFS="," (for input and output as csv file) and I change the columns and the number of decimal... It works but I have also some problems... here my columns ... (7 Replies)
Discussion started by: echo manolis
7 Replies

4. Shell Programming and Scripting

Help with Round Up with 2 decimal point at specific column

Input file: USA 20.5683 UK 3.54221 Japan 2.54001 China 2.50897 Germany 2.05816 . . Desired output file: USA 20.57 UK 3.54 Japan 2.54 China 2.51 Germany 2.06 . . (2 Replies)
Discussion started by: perl_beginner
2 Replies

5. Shell Programming and Scripting

How to round up value upto 2 decimal places using sed?

Please help me in rounding up value upto 2 decimal palces using sed command #!/usr/bin/bash a=15.42 b=13.33 c=`echo $a*$b |bc -l` echo $c above code is is giving output "205.5486" but i want the output as "205.55" Thank you... (15 Replies)
Discussion started by: ranabhavish
15 Replies

6. Shell Programming and Scripting

How to get decimal values ?

Hi All, In my script I've written like this- c=$( expr 100 / 3);echo $c The output coming is 33. but I want to see 33.33, decimal values too. How to get that? Thanks, Naresh (3 Replies)
Discussion started by: NARESH1302
3 Replies

7. Shell Programming and Scripting

Round off the a Decimal value.

HI, I have a script which is used to calculate the Memory & CPU utilization a server. memx=`ssh -l siebel1 ${f} /usr/sbin/prtconf|grep -i 'Memory size'|tr -s " "|/usr/xpg4/bin/awk -F" " '{print $3 * 1024}'` v5=`ssh -l siebel1 ${f} vmstat 1 2 | tail -1 | tr -s " " | /usr/xpg4/bin/awk -v... (3 Replies)
Discussion started by: dear_abhi2007
3 Replies

8. UNIX for Advanced & Expert Users

Converting Binary decimal coded values to Ascii Values

Hi All, Is there any command which can convert binary decimal coded values to ascii values... i have bcd values like below оооооооооооо0о-- -v - Pls suggest a way to convert this. Thanks, Deepti.Gaur (3 Replies)
Discussion started by: gaur.deepti
3 Replies
Login or Register to Ask a Question