On OS X, system_profiler SPHardwareDataType
outputs:
Hardware Overview:
Model Name: MacBook Pro
Total Number of Cores: 4
L2 Cache (per Core): 256 KB
L3 Cache: 8 MB
Memory: 8 GB
I want to get the Memory
value, trimmed of whitespaces.
This is what I had:
system_profiler SPHardwareDataType | grep --color=never -E '^ +Memory: ' | cut -d ':' -f 2
Not ok. It keeps the whitespace.
I switched to this:
system_profiler SPHardwareDataType | perl -ne 'if(/^ +Memory: (.*)$/){print $1;}'
and I'd like to ask:
- Could this be made more concise in Perl?
The braces annoy me, often I type them wrong. Having to put the condition in ()
is also bothersome. The semicolon is bothersome.
- Could this be made as concisely using more basic UNIX tools? (grep/sed/awk)?
Note: I do not use this line I like..
system_profiler SPHardwareDataType | sed -Ene 's/^ +Memory: (.*)$/\1/g p'
because extended regexes (-E
are -r
on some platforms? possible?) and fundamentally because, although I understand that sed
works on lines, -n
suppresses output unless I explicitly p
print it, s//g
is a normal regex substitute
.. and that commands following a match
are only executed on matching lines.. I am puzzled by the fact that s//g
in reality is an action in itself.. so I would expect p
to require a ;
before it.. odd that you can both replace and use the match as a condition to execute the comand.. is that line correct at all?