using awk to strip out decimal point and trailing integers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using awk to strip out decimal point and trailing integers
# 8  
Old 03-11-2012
Hi Guys,

Thanks for your replies (damned internet connection been down *shakes fist*).

Anyway, I've tried the ideas on here but still can't seem to get it to work. If I try the following code:
Code:
USAGEPERCENT=$( awk "BEGIN{print ${ARR[21]} / ${ARR[19]}}" )
USAGEPERCENTTOTAL=$( awk "BEGIN{print int ${USAGEPERCENT1} * 100}" )

Then, I get the following output when I do 'set -x' to see whats going on:
Code:
++ awk 'BEGIN{print 79.436165 / 494.937500}'
+ USAGEPERCENT=0.160497
++ awk 'BEGIN{print int 0.160497 * 100}'
awk: BEGIN{print int 0.160497 * 100}
awk:                 ^ syntax error

Any ideas on how I can get around this ? All the examples above work fine in the command line when using numbers, although I can't get it to work when I attempt to run it in the script when taking values from an array.

Or, is there another way to take the output from my 'USAGEPERCENTTOTAL' and strip everything after the whole number ? I'm not concerned with anything other than the whole number and it's only a rough usage total so no need to carry-forward etc.

[edit]Should have said originally guys, i'm doing this in the bash shell.


Thanx

Jim

Last edited by jimbob01; 03-11-2012 at 03:24 PM.. Reason: Additional information
# 9  
Old 03-11-2012
Hi Jimbob,

Not very good with awk but I thought that integers were whole numbers - are you just trying to get the percentage rounded down to the nearest whole number here?

Regards

Dave
# 10  
Old 03-11-2012
Hi Dave,

Thanks for your reply.

Yeah, that's ideally what I'm trying to do! Later on in my script, i'm using '-gt' to find if the usage is greater than 90%, and it's failing then because '-gt' doesn't work with floating point numbers.

Figured that having a whole number after working out the percentage is easier than changing the IF statement using '-gt'!


Thanx

Jim
# 11  
Old 03-11-2012
Hi Jimbob,

As I said not very good with awk, but I think it should be something like;

Code:
'{printf("%d", 0.160497 * 100)}'

Hope that helps.

Regards

Dave
# 12  
Old 03-11-2012
Hi Dave,

Thanks for your reply.

I've tried that, and it works fine - however, if I substitue the number for my variable then it fails with a syntax error, like:
Code:
'{printf("%d", ${USAGEPERCENT}  * 100)}'

Do you know if it can be done this way ? It's driving me insane!!


Thanx

Jim

Last edited by jimbob01; 03-11-2012 at 04:14 PM.. Reason: Spelling error
# 13  
Old 03-11-2012
Hi Jimbob,

As I said I'm a bit restricted as to what I can do with awk, but it may be the variable itself - I'm not sure if it should be prefixed with the $ and enclosed in {} - you could try it without. I'd try without the braces first.

Sorry to say you may have to wait for an awk GURU to answer the postSmilie

Regards

Dave

Last edited by gull04; 03-11-2012 at 04:20 PM.. Reason: Incomplete
This User Gave Thanks to gull04 For This Post:
# 14  
Old 03-11-2012
Code:
USAGEPERCENT=$( awk "BEGIN{print ${ARR[21]} / ${ARR[19]}}" ) 
USAGEPERCENTTOTAL=$( awk -v p="{USAGEPERCENT}" 'BEGIN{print int (p * 100)}' )

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

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Convert a numeric to 2 decimal point value

Hi , I have a file which contains text like A|Mau|Code|12|Detail B|Mau|Code|20|Header I want to write a command using awk which will output A|Mau|Code|12.00|Detail B|Mau|Code|20.00|Header I used a command like awk -F"|" {printf "%s|%s|%s|%.2f|%s",$1,$2,$3,$4,$5}' which does the... (4 Replies)
Discussion started by: LoneRanger
4 Replies

2. UNIX for Beginners Questions & Answers

How to add following decimal point to a CSV value?

hi there I being trying to figure out way to add " .0" to an integer value in a csv using sed or awk with out success. just as a work around for 2147483647 32 bit limitation that influxdb is currently having the data base will accept values and work fine if it has the XXX.0 ... (7 Replies)
Discussion started by: sash99
7 Replies

3. Shell Programming and Scripting

Check for decimal point and add it at the end if its not there using awk/perl

I have test.dat file with values given below: 20150202,abc,,,,3625.300000,,,,,-5,,,,,,,,,,,,,,,,,,,,,, 20150202,def,,,,32.585,,,,,0,,,,,,,,,,,,,,,,,,,,,, 20150202,xyz,,,,12,,,,,0.004167,,,,,,,,,,,,,,,,,,,,,, My expected output is shown below: ... (1 Reply)
Discussion started by: nvk_vinoth
1 Replies

4. Shell Programming and Scripting

awk decimal point numbers matching

Hi All, Can some one help me in identifying the significance of character "$" ,Which is playing critical role in matching decimal point numbers as below. $ echo "01#.01"|awk '{if ($0 ~ /^+(\.*)?$/) print}' $ echo "01#.01"|awk '{if ($0 ~ /^+(\.*)?/) print}' 01#.01 $ Regards, Rmkganesh. (3 Replies)
Discussion started by: rmkganesh
3 Replies

5. Shell Programming and Scripting

remove directories with two digits after decimal point

Hi everyone, I am new here and generally not experienced with linux. My question must be easy, but as for now I have no idea how to do it. I have lots of directories with numerical names, e.g. 50 50.1 50.12 etc. What I want is to leave directories with no or single digit after the decimal... (2 Replies)
Discussion started by: cabaciucia
2 Replies

6. Shell Programming and Scripting

Insert decimal point for numbers

Hi In Unix, I have a file with some numbers like : 45600 12345 I want to insert a decimal point for these numbers based on user input. If the input is 2, the numbers should be changed to 456.00 123.45 If the input is 3, the numbers should be changed to 45.600 12.345 Can... (2 Replies)
Discussion started by: yoursdivu
2 Replies

7. Shell Programming and Scripting

Comparing two numbers with decimal point

How to compare two numbers with decimal points ? Is there a way in bash to do this? (33 Replies)
Discussion started by: kinny
33 Replies

8. Shell Programming and Scripting

Insert a decimal point

Hi all. Using /bin/sh on an HPUX system. I want to place a decimal in the field 2 charactors from the right (yes, converting to currency). The field lengths are variable. Here's what I'm doing: exec < filename while read FIELD1 FIELD2 do FIELD1="echo $FIELD1 | sed 'syntax that will... (4 Replies)
Discussion started by: lyoncc
4 Replies

9. Shell Programming and Scripting

how to get rid of decimal point?

Hi, I have input with decimal point ( 9.99 ) for hours variable hrs. I need to change it to seconds. Here is my code: secs=`/usr/ucb/echo $hrs*3600 |bc` But I don't want to see the decimal point. I can use awk to trim it if there is one. I am just wondering if there is better standard... (2 Replies)
Discussion started by: cin2000
2 Replies

10. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies
Login or Register to Ask a Question