Linux command for physical memory, getting the value only - linux

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.

Related

How to combine two different linux commands?

I use the following command to get the MAC address of the system.
ifconfig | grep enp0s20f6 | awk '{print $5}'
The following command is used to get the hash of the string:
echo -n "string to be hashed"| md5sum | awk '{print $1}'
I need to get the hashed string for the MAC address by combining both of these commands.
I tried the following, but didn't work.
ifconfig | grep enp0s20f6 | awk '{print $5}' | md5sum
md5sum /sys/class/net/eth0/address | awk '{print $1}'
Try this:
$ cat /sys/class/net/eth0/address | md5sum
Replace eth0 by the name of your interface. Hope it helps! :)
md5sum < /sys/class/net/eth0/address
This command also helps to get the md5sum of mac address

Linux/Terminal Grep Total Memory

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

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).

Calculating Awk Output divide by mega=1048576

Hi Can someone please let me know how I can calculate the output field from this command to MB ?
The command below shows the 20 largest file in directory and sub directories
but I need to convert the output to MB. In my script I use an array.. But If you guys show me how to use awk to divide the output for this by mega=1048576
I would really appreciate it .. Please explain the options !!!
ls -1Rs | sed -e "s/^ *//" | grep "^[0-9]" | sort -nr | head -n20 | awk {'print $1'}
Thanks
You don't show any sample input or expected output so this is a guess but this MAY be what you want (assuming you cant follow all the other good advice about not parsing ls output and you don't have GNU awk for internal sorting):
ls -1Rs | awk '/^ *[0-9]/' | sort -nr | awk 'NR<21{print $1/1024}'
Note that you don't need all those other commands and pipes when you're already using awk.
ls -1Rs | sed -e "s/^ *//" | grep "^[0-9]" | sort -nr | head -n20 | awk {'print $1 / 1024'} To turn it into MB - You have to divide it by 1024

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

Resources