Linux/Terminal Grep Total Memory - linux

I want only the number of my total RAM size.
When I try grep MemTotal /proc/meminfo, I get following:
MemTotal: 3943084 kB
But I want only the number, so i need to replace "MemTotal:" and "kB" with "" (nothing).
How to do this in terminal? (Maybe in one line?)

One approach would be
grep MemTotal /proc/meminfo | awk '{print $2;}'
Which "splits" the input at whitespace and displays only the 2nd word.

On more approach, with only awk use
awk '$1~/MemTotal:/ {print $2;}' /proc/meminfo

Related

Is there a command to output only part of a command result in Linux?

My question is, when we type a command with grep in terminal we get output along with the title:
For example:
lscpu | grep MHz
Will output:
CPU MHz: 1216.851
But what if I only want:
1216.851
As the output? Is there any other command to perform this task?
While there are other ways, the most straightforward would probably be awk:
$ lscpu | grep MHz | awk '{print $3}'
2494.038
Or:
$ lscpu | grep MHz | awk '{print $NF}'
2494.038
$3 represents the third field in the output (separated by any amount of whitespace). $NF represents the LAST field in the output, no matter how many fields there are.
You can also skip grep entirely and just do it all with awk:
$ lscpu | awk '/MHz/ { print $NF; exit }'
2494.038
As #glenn jackman pointed out, GNU grep can also do this:
lscpu | grep --color=never -oP 'MHz:\s+\K.*'
But the other examples above are POSIX-friendly (although systems that have lscpu probably also have GNU grep).

Bash: Show info after grep search term

I'm trying to output information about a machine's system hardware in a bash script. What I'm using is grep to get information about total memory and free memory.
echo -n -e "RAM:\t"
cat /proc/meminfo | grep "MemTotal"
My output shows "MemTotal: 3994850 MB". What I want is just the memory information itself. Is there a way to do this?
Just one awk would be enough instead of multiple piped command here:
awk '/MemTotal/{print $2}' /proc/meminfo
15404808
If you want unit also then use:
awk '/MemTotal/{print $2, $3}' /proc/meminfo
15404808 kB

Calculate percentage free swap space with `free` and `awk`

I'm trying to calculate the percentage of free swap space available.
Using something like this:
free | grep 'Swap' | awk '{t = $2; f = $4; print ($f/$t)}'
but awk is throwing:
awk: program limit exceeded: maximum number of fields size=32767
And I don't really understand why, my program is quite simple, is it possible I'm having a weird range error?
Try this one :
free | grep 'Swap' | awk '{t = $2; f = $4; print (f/t)}'
In your code you are trying to print $f and $t which is respectively $FreeMemory and $TotalMemory. So i guess you have about 4gig ram in total which would refer to ~ $400000 which is a little bit over the total of fields awk uses in standard config. Apart from the easier attempt with meminfo try just printing f/t which refers to the variables and you get your answer.
Note that it might be easier/more robust to read the info by using /proc/meminfo's SwapFree line.
Something like:
$ grep SwapFree /proc/meminfo | awk '{print $2}'
You do not need the variables. You can use plain
awk '{ print $4/$2 }'
Read it from /proc/meminfo:
lennart#trololol:~$ grep SwapFree /proc/meminfo | awk '{print $2}'
0
I realise that the question is about using "free" and "awk", but if you have SAR running, then this will give you the most recently recorded percentage value:
sar -S|tail -2|head -1|awk '{print $5}'

How to print out size of physical memory in linux

If I use this grep MemTotal /proc/meminfo in linux terminal, I get MemTotal: 2059908 kB.
Does anybody know how to get numbers only? which will be 2059908?
One way is to filter the output with something like:
grep MemTotal /proc/meminfo | sed 's/[^0-9]//g'
This will remove all characters that aren't digits, as per the following transcript:
pax:~$ grep MemTotal /proc/meminfo
MemTotal: 4122788 kB
pax:~$ grep MemTotal /proc/meminfo | sed 's/[^0-9]//g'
4122788
Try adding this: |awk '{print $2}'
besides looking at /proc/meminfo, any of the following commands on LINUX will help: free, top

Linux command for physical memory, getting the value only

In Linux cat /proc/meminfo | grep MemTotal gets back MemTotal: 12298824 kB
I want only the numbers here
so i wrote cat /proc/meminfo | grep MemTotal | cut -d':' -f2 which gave me 12298824 kB
I only want the numbers here, can anyone help me?
Note: cat /proc/meminfo | grep MemTotal | cut -d':' -f2 | cut -d'k' -f1 gives me the solution 12298824, but is there a better way? one liner?
Use awk:
cat /proc/meminfo | grep MemTotal | awk '{print $2}'
From #Lars Wirzenius's comment(No cat and No grep):
awk '/MemTotal/ { print $2 }' /proc/meminfo
I have used:
dmidecode -t 17 | grep Size | awk '{s+=$2} END {print s}'
to great effect in my CentOS kickstart %pre section. It returns the total installed amount of memory in MB. Even if you have empty memory banks, awk will ignore them and only add the integer results.

Resources