Link to home
Start Free TrialLog in
Avatar of digs developer
digs developer

asked on

Unix shell script, How to do rounding of number ?

Below is my code. I want to check total number of lines and divided it by 2.

Suppose total no of lines =24 then I will divide by 2 = 12 .

But  when total no of lines =23 and then I will divide by 2 it gives 11 . I want 12 because actulay value is 11.5


ncnt=$((`wc -l < AMS_Total_Business_ID.txt` / 2))

Please help.
Avatar of ozo
ozo
Flag of United States of America image

ncnt=$(( ( `wc -l < AMS_Total_Business_ID.txt` + 1 ) / 2))
Avatar of digs developer
digs developer

ASKER

But when I divide by 1000 is is not shown correct nmber like by 2.


ncnt=$(( ( `wc -l < AMS_Total_Business_ID.txt` + 1 ) / 1000))

division by value should be change on run time. It may be 500 ,900 or 1000

Please help.
ncnt=$(( ( `wc -l < AMS_Total_Business_ID.txt` + 500 ) / 1000))
Thank you !
I have one text file ABC_T_B_ID.txt which contains values in one column.
The values may be contain 1 or 1000 or 45000 and so on.

So my requirement is count the total line in a file and divided it by 1000 whatever the value comes that value should be ceil.

suppose
1. no of lines = 1000
   1000/1000 = 1
   
2. no of lines = 2000
   2000/1000 = 2

3. no of lines = 10
   10/1000 = 0.01   I need as 1  
   
4. no of lines = 411
   411/1000 = 0.411   I need as 1  

4. no of lines = 9898
   9898/1000 = 9.898   I need as 10    
   
4. no of lines = 9001
   9001/1000 = 9.001   I need as 10    
   
Below is my code.
Please help.  



ncnt=$(( ( `wc -l < ABC_T_B_ID.txt` + 500 ) / 1000))
Please help
Hello Experts,

Please need help
Seems you always want to round up:

ncnt=$(( ( `wc -l < ABC_T_B_ID.txt` + 999 ) / 1000))

More generally:

DIV=1000
ncnt=$(( ( `wc -l < ABC_T_B_ID.txt` + ($DIV-1) ) / $DIV))

Note Always rounding up will (for example) give 46 for 45001 lines divided by 1000. Is this desired?
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial