Operating System - Linux
1826201 Members
2474 Online
109691 Solutions
New Discussion

Triming characters from output

 

Triming characters from output

Hi,

I am using the following command to get total memory:

# top -n 1 | grep Mem | awk '{print $2}'

I get the following output:
126112K

How do I further trim this output to get rid of the K so that I can use this number in a calculation.

Thanks,
Trystan.
4 REPLIES 4
Jerome Henry
Honored Contributor

Re: Triming characters from output

Why not
# top -n 1 | grep Mem | awk '{print $2*1024}'
?
It works...
J
You can lean only on what resists you...
Sergejs Svitnevs
Honored Contributor

Re: Triming characters from output

top -n 1 | grep Mem | awk '{print $2-0}'

Regards,
Sergejs
Claudio Cilloni
Honored Contributor

Re: Triming characters from output

if getting memory information is your target,
you can find the file
/proc/meminfo
more useful.

$ cat /proc/meminfo | grep MemTotal | awk '{print $2}'
512596

it's even faster than using top.

hope this helps,

Claudio
Bill Douglass
Esteemed Contributor

Re: Triming characters from output

Or to get it in bytes (again with /proc/meminfo)

cat /proc/meminfo | grep ^Mem: | awk '{print $2}'