Retrieve RAM memory size from "top" command output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Retrieve RAM memory size from "top" command output
# 1  
Old 02-07-2011
Retrieve RAM memory size from "top" command output

Hi,

I am trying to get the system RAM size from "top" command's output by the following but it is not working.

Code:
top | sed "s/^Mem.*[^0-9]*\([0-9]*\), *//"


Last edited by royalibrahim; 02-07-2011 at 09:31 AM..
# 2  
Old 02-07-2011
Post the output of your top command and the desired output.
# 3  
Old 02-07-2011
Code:
# top | awk '/^Mem/ {print $2}'
32G

(If Solaris. But why use top?)

Linux.
Code:
$ top -n1 | awk '/^Mem/ {print $3}'
1026880k


Last edited by Scott; 02-07-2011 at 09:53 AM..
# 4  
Old 02-08-2011
Quote:
Originally Posted by Franklin52
Post the output of your top command and the desired output.
The output is as follows:
Code:
top - 09:45:10 up 15:48,  1 user,  load average: 0.07, 0.02, 0.00
Tasks: 112 total,   1 running, 111 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   1025740k total,  1012624k used,    13116k free,   428188k buffers
Swap:  3004112k total,    74580k used,  2929532k free,   134616k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
    1 root      20   0  2848 1688  544 S    0  0.2   0:02.12 init
    2 root      15  -5     0    0    0 S    0  0.0   0:00.00 kthreadd


Quote:
Originally Posted by scottn
Code:
# top | awk '/^Mem/ {print $2}'
32G

(If Solaris. But why use top?)

Linux.
Code:
$ top -n1 | awk '/^Mem/ {print $3}'
1026880k

Thanks Scottn, but is it possible to do it via 'sed'??
# 5  
Old 02-08-2011
Right tool for the right job and all that. You want to match a line and extract the third token? Well awk matches lines, and extracts tokens. You'd need to write a big monster of an expression to tell sed what you mean by 'token'.
# 6  
Old 02-08-2011
Quote:
Originally Posted by Corona688
Right tool for the right job and all that. You want to match a line and extract the third token? Well awk matches lines, and extracts tokens. You'd need to write a big monster of an expression to tell sed what you mean by 'token'.
But we can map any sequence of characters with '.*' right in Sed? Where I am going wrong in my code?
# 7  
Old 02-08-2011
Are you looking for an output like this.. Mem: 1025740k from the top command..?
Code:
top | sed -n '/Mem/s/ [a-z]\+.*//p'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

4. UNIX for Dummies Questions & Answers

Explanation of "total" field in "ls -l" command output

When I do a listing in one particular directory (ls -al) I get: total 43456 drwxrwxrwx 2 root root 4096 drwxrwxrwx 3 root root 4096 -rwxrwxr-x 1 nobody nobody 3701594 -rwxrwxr-x 1 nobody nobody 3108510 -rwxrwxr-x 1 nobody nobody 3070580 -rwxrwxr-x 1 nobody nobody 3099733 -rwxrwxr-x 1... (1 Reply)
Discussion started by: proactiveaditya
1 Replies

5. Shell Programming and Scripting

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

6. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

7. UNIX for Dummies Questions & Answers

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

8. Fedora

Piping output of "top" to a text file

I would like to pipe "top -n" to a text file, but I get an error: top: cannot open /dev/kmem kvm_open: Permission denied I am a non-root user. If I could find a way to get this type of output: "Memory: 2048M real, 1516M free, 4099M swap free" into a text file, I could further automate... (13 Replies)
Discussion started by: safraser
13 Replies

9. Debian

Debian: doubt in "top" %CPU and "sar" output

Hi All, I am running my application on a dual cpu debian linux 3.0 (2.4.19 kernel). For my application: <sar -U ALL> CPU %user %nice %system %idle ... 10:58:04 0 153.10 0.00 38.76 0.00 10:58:04 1 3.88 0.00 4.26 ... (0 Replies)
Discussion started by: jaduks
0 Replies

10. UNIX for Dummies Questions & Answers

Command for "Resident memory Size"

Hi Everyone.- Coul you tell me how to obtain the "Resident memory size" for any program runs on Dec TRU64 machine. What is the command in order to obtaing this value?? Best Regards Cristobal (1 Reply)
Discussion started by: Cristobal Perez
1 Replies
Login or Register to Ask a Question