LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-14-2015, 01:46 AM   #1
aristosv
Member
 
Registered: Dec 2014
Posts: 138

Rep: Reputation: Disabled
Changing the output of CPU and RAM usage


I am using the following two commands to output CPU and RAM usage on a Linux machine.

Code:
/bin/grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'

/usr/bin/free | grep Mem | awk '{print $3/$2 * 100.0 "%"}'
My problem is that the output is like this

Quote:
5.33672%
13.9723%
Is there a way to output a single number? For example

Quote:
5
13
Thank you
 
Old 02-14-2015, 02:39 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,057

Rep: Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916
Use printf (with format modifiers) rather than print.
There is also no need for grep - awk can handle all required logic.
 
Old 02-14-2015, 02:49 AM   #3
linuxtech99
Member
 
Registered: Jan 2015
Posts: 35

Rep: Reputation: 4
There is no easy way to do this. But we can achieve with printf

CPU
Code:
s=$(/bin/grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}' | cut -d "%" -f1); echo "$(printf "%0.0f\n" $s)";s="";
Memory
Quote:
s=$(/usr/bin/free | grep Mem | awk '{print $3/$2 * 100.0 "%"}' | cut -d "%" -f1);echo "$(printf "%0.0f\n" $s)";s="";
 
Old 02-14-2015, 03:01 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,057

Rep: Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916
awk also supports printf - there is no need for any other external program in this case - no grep, sed, cut or anything else.
 
Old 02-14-2015, 05:18 AM   #5
aristosv
Member
 
Registered: Dec 2014
Posts: 138

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by linuxtech99 View Post
There is no easy way to do this. But we can achieve with printf

CPU
Code:
s=$(/bin/grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}' | cut -d "%" -f1); echo "$(printf "%0.0f\n" $s)";s="";
Memory
thank man, works great.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Squid 3 near 100% cpu usage and high RAM usage piman Linux - Software 1 11-16-2013 03:20 AM
Optimize RAM and CPU usage cccc Debian 11 09-17-2010 02:12 AM
CPU and RAM usage rslinks Linux - Server 3 10-06-2008 02:39 PM
ram/cpu usage etc... program Ken~ Linux - Software 2 08-28-2007 06:38 PM
Ram and CPU usage history log odegos Linux - Enterprise 4 05-21-2007 03:11 PM


All times are GMT -5. The time now is 07:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration