How to restrict awk math output to 2 decimal places
My Command is below and its print Free CPU and Used CPU below format.
I want to restrict awk output to 2 decimal place.
grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} {free=($5)*100/($2+$3+$4+$5)} END {print "Used Cpu:",usage "%"} {print "Free Cpu:",free "%"}'
Output :
Free Cpu: 98.9588%
Used Cpu: 1.04125%
Desired output :
Free Cpu: 98.95%
Used Cpu: 1.04%
linux awk cpu
New contributor
add a comment |
My Command is below and its print Free CPU and Used CPU below format.
I want to restrict awk output to 2 decimal place.
grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} {free=($5)*100/($2+$3+$4+$5)} END {print "Used Cpu:",usage "%"} {print "Free Cpu:",free "%"}'
Output :
Free Cpu: 98.9588%
Used Cpu: 1.04125%
Desired output :
Free Cpu: 98.95%
Used Cpu: 1.04%
linux awk cpu
New contributor
2
use printf:printf "Used Cpu: %.2f%%n", usage
– glenn jackman
12 hours ago
1
Note, your END block only prints "Used Cpu" -- the block that prints "Free Cpu" is not in the END block, and wil therefore print for each line of input.
– glenn jackman
12 hours ago
add a comment |
My Command is below and its print Free CPU and Used CPU below format.
I want to restrict awk output to 2 decimal place.
grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} {free=($5)*100/($2+$3+$4+$5)} END {print "Used Cpu:",usage "%"} {print "Free Cpu:",free "%"}'
Output :
Free Cpu: 98.9588%
Used Cpu: 1.04125%
Desired output :
Free Cpu: 98.95%
Used Cpu: 1.04%
linux awk cpu
New contributor
My Command is below and its print Free CPU and Used CPU below format.
I want to restrict awk output to 2 decimal place.
grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} {free=($5)*100/($2+$3+$4+$5)} END {print "Used Cpu:",usage "%"} {print "Free Cpu:",free "%"}'
Output :
Free Cpu: 98.9588%
Used Cpu: 1.04125%
Desired output :
Free Cpu: 98.95%
Used Cpu: 1.04%
linux awk cpu
linux awk cpu
New contributor
New contributor
edited 5 mins ago
Isaac
11.6k11752
11.6k11752
New contributor
asked 12 hours ago
Sandeep SinghSandeep Singh
1
1
New contributor
New contributor
2
use printf:printf "Used Cpu: %.2f%%n", usage
– glenn jackman
12 hours ago
1
Note, your END block only prints "Used Cpu" -- the block that prints "Free Cpu" is not in the END block, and wil therefore print for each line of input.
– glenn jackman
12 hours ago
add a comment |
2
use printf:printf "Used Cpu: %.2f%%n", usage
– glenn jackman
12 hours ago
1
Note, your END block only prints "Used Cpu" -- the block that prints "Free Cpu" is not in the END block, and wil therefore print for each line of input.
– glenn jackman
12 hours ago
2
2
use printf:
printf "Used Cpu: %.2f%%n", usage
– glenn jackman
12 hours ago
use printf:
printf "Used Cpu: %.2f%%n", usage
– glenn jackman
12 hours ago
1
1
Note, your END block only prints "Used Cpu" -- the block that prints "Free Cpu" is not in the END block, and wil therefore print for each line of input.
– glenn jackman
12 hours ago
Note, your END block only prints "Used Cpu" -- the block that prints "Free Cpu" is not in the END block, and wil therefore print for each line of input.
– glenn jackman
12 hours ago
add a comment |
2 Answers
2
active
oldest
votes
A bit of code review:
- awk can do what grep does, so you don't need the pipeline
- add newlines to the awk code for reability
For example:
awk '$1 == "cpu" {
printf "Used Cpu: %5.2f%%n", 100*($2+$4)/($2+$4+$5)
printf "Free Cpu: %5.2f%%n", 100*$5/($2+$3+$4+$5)
}' /proc/stat
which outputs the nicely aligned
Used Cpu: 5.34%
Free Cpu: 94.63%
add a comment |
An alternative way is to define an output format OFMT="%5.2f%%"
to format all numeric output:
$ awk -v OFMT="%5.2f%%" '
$1 == "cpu" {
print( "Used Cpu: ", 100*($2+$4)/($2+$4+$5) )
print( "Free Cpu: ", 100*$5/($2+$3+$4+$5) )
}
' /proc/stat
Used Cpu: 9.52%
Free Cpu: 90.48%
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sandeep Singh is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496906%2fhow-to-restrict-awk-math-output-to-2-decimal-places%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
A bit of code review:
- awk can do what grep does, so you don't need the pipeline
- add newlines to the awk code for reability
For example:
awk '$1 == "cpu" {
printf "Used Cpu: %5.2f%%n", 100*($2+$4)/($2+$4+$5)
printf "Free Cpu: %5.2f%%n", 100*$5/($2+$3+$4+$5)
}' /proc/stat
which outputs the nicely aligned
Used Cpu: 5.34%
Free Cpu: 94.63%
add a comment |
A bit of code review:
- awk can do what grep does, so you don't need the pipeline
- add newlines to the awk code for reability
For example:
awk '$1 == "cpu" {
printf "Used Cpu: %5.2f%%n", 100*($2+$4)/($2+$4+$5)
printf "Free Cpu: %5.2f%%n", 100*$5/($2+$3+$4+$5)
}' /proc/stat
which outputs the nicely aligned
Used Cpu: 5.34%
Free Cpu: 94.63%
add a comment |
A bit of code review:
- awk can do what grep does, so you don't need the pipeline
- add newlines to the awk code for reability
For example:
awk '$1 == "cpu" {
printf "Used Cpu: %5.2f%%n", 100*($2+$4)/($2+$4+$5)
printf "Free Cpu: %5.2f%%n", 100*$5/($2+$3+$4+$5)
}' /proc/stat
which outputs the nicely aligned
Used Cpu: 5.34%
Free Cpu: 94.63%
A bit of code review:
- awk can do what grep does, so you don't need the pipeline
- add newlines to the awk code for reability
For example:
awk '$1 == "cpu" {
printf "Used Cpu: %5.2f%%n", 100*($2+$4)/($2+$4+$5)
printf "Free Cpu: %5.2f%%n", 100*$5/($2+$3+$4+$5)
}' /proc/stat
which outputs the nicely aligned
Used Cpu: 5.34%
Free Cpu: 94.63%
answered 12 hours ago
community wiki
glenn jackman
add a comment |
add a comment |
An alternative way is to define an output format OFMT="%5.2f%%"
to format all numeric output:
$ awk -v OFMT="%5.2f%%" '
$1 == "cpu" {
print( "Used Cpu: ", 100*($2+$4)/($2+$4+$5) )
print( "Free Cpu: ", 100*$5/($2+$3+$4+$5) )
}
' /proc/stat
Used Cpu: 9.52%
Free Cpu: 90.48%
add a comment |
An alternative way is to define an output format OFMT="%5.2f%%"
to format all numeric output:
$ awk -v OFMT="%5.2f%%" '
$1 == "cpu" {
print( "Used Cpu: ", 100*($2+$4)/($2+$4+$5) )
print( "Free Cpu: ", 100*$5/($2+$3+$4+$5) )
}
' /proc/stat
Used Cpu: 9.52%
Free Cpu: 90.48%
add a comment |
An alternative way is to define an output format OFMT="%5.2f%%"
to format all numeric output:
$ awk -v OFMT="%5.2f%%" '
$1 == "cpu" {
print( "Used Cpu: ", 100*($2+$4)/($2+$4+$5) )
print( "Free Cpu: ", 100*$5/($2+$3+$4+$5) )
}
' /proc/stat
Used Cpu: 9.52%
Free Cpu: 90.48%
An alternative way is to define an output format OFMT="%5.2f%%"
to format all numeric output:
$ awk -v OFMT="%5.2f%%" '
$1 == "cpu" {
print( "Used Cpu: ", 100*($2+$4)/($2+$4+$5) )
print( "Free Cpu: ", 100*$5/($2+$3+$4+$5) )
}
' /proc/stat
Used Cpu: 9.52%
Free Cpu: 90.48%
answered 9 mins ago
IsaacIsaac
11.6k11752
11.6k11752
add a comment |
add a comment |
Sandeep Singh is a new contributor. Be nice, and check out our Code of Conduct.
Sandeep Singh is a new contributor. Be nice, and check out our Code of Conduct.
Sandeep Singh is a new contributor. Be nice, and check out our Code of Conduct.
Sandeep Singh is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496906%2fhow-to-restrict-awk-math-output-to-2-decimal-places%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
use printf:
printf "Used Cpu: %.2f%%n", usage
– glenn jackman
12 hours ago
1
Note, your END block only prints "Used Cpu" -- the block that prints "Free Cpu" is not in the END block, and wil therefore print for each line of input.
– glenn jackman
12 hours ago