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
# 8  
Old 02-08-2011
Hi.

As noted, it is a lot more work to extract a field with sed compared to awk. Here's one solution:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate complexity of sed compared to awk.

# Utility functions: print-as-echo, print-line-with-visual-space.
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }

FILE=${1-data1}

pl " Input file $FILE:"
cat $FILE

pl " Results, sed:"
sed -n '/^Mem:/s/^[^0-9]*\([0-9]*.\).*$/\1/p' $FILE

exit 0

producing:
Code:
% ./s1

-----
 Input file data1:
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

-----
 Results, sed:
1025740k

If you don't want the "k", omit the "dot" in the expression.

Best wishes ... drl

( Edit 1: minor typo )

Last edited by drl; 02-08-2011 at 10:09 AM..
# 9  
Old 02-09-2011
Quote:
Originally Posted by michaelrozar17
Are you looking for an output like this.. Mem: 1025740k from the top command..?
Code:
top | sed -n '/Mem/s/ [a-z]\+.*//p'

Thanks michaelrozar17, but your solution is not giving me any output in my Ubuntu 8.04 Desktop edition 32-bit x86 machine Smilie



Also, drl, your code:
Code:
top |  sed -n '/^Mem:/s/^[^0-9]*\([0-9]*.\).*$/\1/p'

gives output as 39; in my system.. why?

Last edited by royalibrahim; 02-09-2011 at 12:54 AM..
# 10  
Old 02-09-2011
Quote:
Originally Posted by royalibrahim
Thanks michaelrozar17, but your solution is not giving me any output in my Ubuntu 8.04 Desktop edition 32-bit x86 machine Smilie
The given sed works if the top commands output looks exactly as in post# 4. However i have tried in my machine with the top | sed .. it works!. Else try with awk.
Code:
top | awk '/Mem/{print "Mem: " $2}'

Also remember there is a single space in between s/ and [a-z] in the sed command without which you would not get the expected output.

Last edited by michaelrozar17; 02-09-2011 at 03:49 AM.. Reason: typo..
# 11  
Old 02-09-2011
Quote:
Originally Posted by michaelrozar17
Are you looking for an output like this.. Mem: 1025740k from the top command..?
Code:
top | sed -n '/Mem/s/ [a-z]\+.*//p'

Oh, now I got to know that why I was not getting any output for your solution and output 39; for drl. By default the "top" command outputs the result with escape sequences which is not readable and cannot be processed normally by any filter commands. So, the output has to be changed to batch mode using "-b" option before feeding into 'sed'

Below command works perfectly:
Code:
top -b -n1 | sed -n '/Mem/s/ [a-z]\+.*//p'

Thanks a lot for all your replies Smilie
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