Bash: Show info after grep search term - linux

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

Related

Can I pipe lshw warnings to /dev/null when I run it as a standard user?

I'm trying to create an alias for getting memory on my machine, currently I have alias mem="lshw | grep size | awk -F: '{print $2}'", and when I run it as a non-super user, I get the following warning message:
WARNING: you should run this program as super-user.
WARNING: output may be incomplete or inaccurate, you should run this program as super-user.
size: 23GiB
I'm not worried about the results being potentially incomplete, in fact when I diff the output when running as root vs a standard user, it's exactly the same. Does anybody know how to get rid of these warnings? I tried piping stderr to /dev/null, but that didn't work. Does anyone else know how to get rid of these warnings?
Can I interest you in
alias mem='free -g | grep Mem | awk '\''{print $2 " GiB"}'\'
free -m will give MiB; you can change the " GiB" part to whatever you want (or remove it).
I don't have lshw installed on my machine, so I can't help you debug your version, unfortunately.
alias mem="lshw 2> /dev/null| grep size | awk -F: '{print $2}'"
Alternatively you can use free or read from /proc/meminfo
cat /proc/meminfo |grep MemTotal
I'm not sure how you piped to dev/null, but this works for me:
lshw 2> /dev/null | grep size | awk -F: '{print $2}'
Ignoring that there are other tools more suited to getting the memory, if there is something you need and lshw is your only option, you would be better suited to use -json or -xml output and use a tool to parse it like jq or xmllint. The version of lshw on my distro outputs invalid json that can't be parsed, but does have valid xml output.
This would accomplish your goal, although the path may very well be different for you:
lshw -xml 2> /dev/null | xmllint --xpath '/list/node/node/node[#id="memory"]/size/text()' -
Or add a one grep:
... | grep "size:"

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

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

How to get the total physical memory in Bash to assign it to a variable?

How can I get the total physical memory in bytes of my Linux PC?
I need to assign it to a bash script variable.
grep MemTotal /proc/meminfo | awk '{print $2}'
The returned number is in KB
phymem=$(awk -F":" '$1~/MemTotal/{print $2}' /proc/meminfo )
or using free
phymem=$(LANG=C free|awk '/^Mem:/{print $2}')
or using shell
#!/bin/bash
while IFS=":" read -r a b
do
case "$a" in
MemTotal*) phymem="$b"
esac
done <"/proc/meminfo"
echo $phymem
I came up with this one under the assumption, that the physical memory will be the first number in free's output:
free -m | grep -oP '\d+' | head -n 1
This allows you to configure free to output the unit you want (-m, -g, ...) and it is independent of the system language (other answers depend on the "Mem:" string in free's output which may change based on the language).
How about
var=$(free | awk '/^Mem:/{print $2}')
I'll try to make this answer self explanatory, just keep up with me.
To get the description of memory, you can use the free utility :
free -t
Output (in KB):
total used free shared buff/cache available
Mem: 8035900 3785568 324984 643936 3925348 3301908
Swap: 3906556 271872 3634684
Total: 11942456 4057440 3959668
To extract all of these values from this output in a single column :
free -t | grep -oP '\d+'
Output (in KB):
8035900
3866244
266928
650348
3902728
3214792
3906556
292608
3613948
11942456
4158852
3880876
Note : Minute difference can be there in values, which doesn't matter most of the times.
If you just want to get the total physical memory (mem+swap), it is the 10th value in above output :
free -t | grep -oP '\d+' | sed '10!d'
Output (on my PC):
11942456
Note: All the above outputs are in Kilo Bytes. If you want in Mega Bytes or Giga Bytes just append -m or -g after -t in
above free commands respectively.
For Example :
free -t -g | grep -oP '\d+' | sed '10!d'
Output (in Giga Bytes on my PC) :
11
Silly inline python version, which looks overly complicated, but is actually kind of useful.
freemem=$(echo -e 'import re\nmatched=re.search(r"^MemTotal:\s+(\d+)",open("/proc/meminfo").read())\nprint(int(matched.groups()[0])/(1024.**2))' | python)
It returns the memory in GB.
If someone need a human readable:
var=$(free -h | awk '/^Mem:/{print $2}')
result:
1.9G

Resources