Converting awk printf string to decimal

murloc

Task:

stdout the load avg from top in a decimal form like (e.g 0.23)

Details:

I need to parse this script to Chef's inspec and check if its result is bigger/smaller than something, for example:

describe command("top -b -n  1  | awk  '/load average/ { sub(/,/,\".\",$10); printf \"%f\n\",$10}'") do
  its("stdout") { should eq 0.00 }    
end

This example returns ""

But now when I think about it, I could compare with a file in /proc/loadavg

Progress

Used this resource: Grab the load average with top

With this command, I get a good representation of the output, but it's a string and I can't do any mathematical operations with it:

martin@martinv-pc:~$ top -b -n  1  | awk '/load average/ { printf "%s\n", $10}'
0,63,

But when I try to change the printf to decimal/float, I get an error:

martin@martinv-pc:~$ top -b -n  1  | awk '/load average/ { printf "%f\n", $10}'
0.000000
martin@martinv-pc:~$ top -b -n  1  | awk '/load average/ { printf "%d\n", $10}'
0

Can't echo, tried with cut, bad idea -- not working:

martin@martinv-pc:~$ top -b -n  1  | awk '/load average/ { printf "%s\n", $10}'|cut -c1-4
0,15
martin@martinv-pc:~$ top -b -n  1  | awk '/load average/ { printf "%s\n", $10}'|$((cut -c1-4))
-4: command not found

Another attempt:

martin@martinv-pc:~$ top -b -n  1  | awk '/load average/ BEGIN { printf "%.f\n", $10};'
awk: line 1: syntax error at or near BEGIN

Question:

How can I convert the string value to decimal/float/integer ?

ps -o user,rss output:

[vagrant@localhost ~]$ ps -o user,rss
USER       RSS
vagrant    736
vagrant   1080
simlev

Task:

sdout the load avg from top in a decimal form like(e.g 0.23)

Solution:

top -b -n  1  | perl -lane 'print "$1.$2" if /load average: (\d+)[,.](\d+)/'

Notes:

This retrieves the 1m load average. It looks like this is what you want, but you should state it clearly. If needed, the code can be easily modified to retrieve the load averages over 5 minutes or 15 minutes, or even all three.

As pointed out by @terdon, uptime might be a better starting point than top in this case.

After the first two lines, you obscurely describe what you want to do with the result. Subsequent steps you want to take should be the subject of new questions.

In Perl, numbers are auto-casted to strings and vice-versa. Any numerical operation can be performed on a string representing a number. e.g. print "$1.$2"+11.11


Question 2:

This part is about the second question, which is totally unrelated to the first one.
I urge the OP to post this question separately.

How Can I convert the string value to decimal/float/integer ?

Better written as: Performing numeric comparisons on strings with Chef's InSpec.

Solution:

Convert the string to a numeric format, with either to_i or to_f.

Example:

describe command("echo 1.00") do
    its("stdout.to_f") { should be < 1.03 }
end

Explanation:

Very reasonably, stdout is treated as a string. Also very reasonably, numeric comparisons require the two numbers to be...numbers. Luckily, conversion can be done with the handy Ruby string methods: to_i, to_f, to_r and to_c.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Converting string "decimal" to DecimalType

From Dev

Converting Hexadecimal String to Decimal Integer

From Dev

Converting Specific String/Integer into Decimal

From Dev

Converting a big integer to decimal string

From Dev

Converting a hexadecimal string to a decimal integer

From Dev

Error converting decimal to binary in string

From Dev

Converting a big integer to decimal string

From Dev

Converting string value with comma to Decimal with correct Locale

From Dev

Converting Double to String with desired decimal separator

From Dev

Converting string to Percent with 2 decimal points

From Dev

Converting string value with comma to Decimal with correct Locale

From Dev

Csharp - Convert single to decimal without converting to a string

From Dev

Add a decimal (.) to a String before converting into an int

From Dev

Converting To String Removes 0's after Decimal

From Dev

Converting string to decimal: how to handle the decimal separator in different cultures

From Dev

Converting to decimal

From Dev

Converting string to decimal if string isNumeric in SSRS to calculated weighted average

From Dev

iOS - Decimal Value Changed After Converting a JSON String Into a JSON Object

From Dev

Converting a very small python Decimal into a non-scientific notation string

From Dev

Converting string into Decimal value by using C# OR BizTalk Functoids

From Dev

Converting a decimal (user string) into hex with 0x prefix

From Dev

Converting decimal number with negative sign to fixed width string

From Dev

Converting String of binary digits to decimal number... using recursion

From Dev

Converting string with points into decimal number in objective-c

From Dev

Converting string with decimal to double using atof in C++

From Dev

Converting String of binary digits to decimal number... using recursion

From Dev

Binary string isn't converting to decimal properly on my Arduino

From Dev

Converting a column from decimal to binary string in Spark / Scala

From Dev

Stuck converting decimal with decimal values

Related Related

  1. 1

    Converting string "decimal" to DecimalType

  2. 2

    Converting Hexadecimal String to Decimal Integer

  3. 3

    Converting Specific String/Integer into Decimal

  4. 4

    Converting a big integer to decimal string

  5. 5

    Converting a hexadecimal string to a decimal integer

  6. 6

    Error converting decimal to binary in string

  7. 7

    Converting a big integer to decimal string

  8. 8

    Converting string value with comma to Decimal with correct Locale

  9. 9

    Converting Double to String with desired decimal separator

  10. 10

    Converting string to Percent with 2 decimal points

  11. 11

    Converting string value with comma to Decimal with correct Locale

  12. 12

    Csharp - Convert single to decimal without converting to a string

  13. 13

    Add a decimal (.) to a String before converting into an int

  14. 14

    Converting To String Removes 0's after Decimal

  15. 15

    Converting string to decimal: how to handle the decimal separator in different cultures

  16. 16

    Converting to decimal

  17. 17

    Converting string to decimal if string isNumeric in SSRS to calculated weighted average

  18. 18

    iOS - Decimal Value Changed After Converting a JSON String Into a JSON Object

  19. 19

    Converting a very small python Decimal into a non-scientific notation string

  20. 20

    Converting string into Decimal value by using C# OR BizTalk Functoids

  21. 21

    Converting a decimal (user string) into hex with 0x prefix

  22. 22

    Converting decimal number with negative sign to fixed width string

  23. 23

    Converting String of binary digits to decimal number... using recursion

  24. 24

    Converting string with points into decimal number in objective-c

  25. 25

    Converting string with decimal to double using atof in C++

  26. 26

    Converting String of binary digits to decimal number... using recursion

  27. 27

    Binary string isn't converting to decimal properly on my Arduino

  28. 28

    Converting a column from decimal to binary string in Spark / Scala

  29. 29

    Stuck converting decimal with decimal values

HotTag

Archive