What is the purpose of characters ".*" in command "sudo dmicode -t 17 | grep "Size.*MB"?
The answer to the physical RAM question is:
sudo dmidecode -t 17 | grep "Size.*MB" | awk '{s+=$2} END {print s / 1024 "GB"}'
Above code gives the correct answer of 8GB on this computer with Unbutu 20.04.3
Please explain why?
There are 4 records (4 rows) with this dmidecode
command that greps with "MB:
sudo dmidecode -t 17 | grep "Size.*MB"
Size: 2048 MB
Size: 2048 MB
Size: 2048 MB
Size: 2048 MB
There are 5 records (5 rows) this dmidecode
command that greps with "Size":
sudo dmidecode -t 17 | grep "Size"
Size: 2048 MB
Size: 2048 MB
Size: 2048 MB
Size: 2048 MB
Size: 4096 kB
There are 0 records (zero) with:
sudo dmidecode -t 17 | grep "Size**MB"
sudo dmidecode -t 17 | grep "Size*MB"
sudo dmidecode -t 17 | grep "Size.?MB"
sudo dmidecode -t 17 | grep "Size. MB"
sudo dmidecode -t 17 | grep "Size??MB"
sudo dmidecode -t 17 | grep "Size???MB"
sudo dmidecode -t 17 | grep "Size????MB"
Can someone explain how we get 4 records with:
sudo dmidecode -t 17 | grep "Size.*MB"
The 4 records correctly shows the 4 memory sticks of 2048 MB per stick.
Then awk addition correctly gives:
8192 MB = 2048+2048+2048+2048
Then awk division by 1024 we have 8GB.
Can someone clarify what the characters .*
are doing in above?
The answer's in man grep
: REGEX.
As per grep manpage:
The period . matches any single character.
* The preceding item will be matched zero or more times.
Command grep "Size.*MB"
translates to:
Print all lines containing exact pattern "Size" followed by exact pattern "MB", regardless of how many characters are between the patterns.
The command would also display lines:
SizeMB
Size do-be do-be doo MB
easd EdsSizei up@#$Ahfu e8MBGj;eh di uh7987 p*&(8