Learn how to use cat, grep, cut, sed and awk to get data about your system.
We will be trying to get the "Total usable memory" from the /proc/meminfo file just to demonstrate this.
Here are the commands used in this video.
cat /proc/meminfo
cat /proc/meminfo | grep MemTotal
cat /proc/meminfo | grep MemTotal | cut -d: -f 2
cat /proc/meminfo | grep MemTotal | cut -d: -f 2 | sed 's/^ *//g'
^ denotes that what appears after the ^ must appear at the beginning of the pattern space.
$ is the same as ^, but refers to end of pattern space. $ also acts as a special character only at the end of the regular expression or subexpression (that is, before \) or \|), and its use at the end of a subexpression is not portable.
* means what matches a sequence of zero or more instances of matches for the preceding regular expression
cat /proc/meminfo | grep MemTotal | cut -d: -f 2 | sed 's/^ *//g' | cut -d ' ' -f 1
cat /proc/meminfo | grep MemTotal | cut -d: -f 2 | sed 's/^ *//g' | cut -d ' ' -f 1 | awk '{print $0*1024}'
cat /proc/meminfo | grep MemTotal | cut -d: -f 2 | sed 's/^ *//g' | cut -d ' ' -f 1 | awk '{print $0*1024" bytes"}'
cat /proc/meminfo | grep MemTotal | cut -d: -f 2 | sed 's/^ *//g' | cut -d ' ' -f 1 | awk '{print $0*1024" bytes"}' abc.txt
YOUTUBE
#linux #ubuntu #bash #shell #script