Get OSX codename from command line
I'm sure there's got to be an easier and more-reliable way, but at least you can eliminate the pipe to sed
altogether by using grep
with -o
(prints only matches) and -E
(extended regular expressions):
grep -oE 'SOFTWARE LICENSE AGREEMENT FOR OS X.*[A-Z]' '/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/en.lproj/OSXSoftwareLicense.rtf'
This does, however, also return the SOFTWARE LICENSE AGREEMENT FOR OS X
portion of the output. If you just want the codename, you could pipe it to sed, but it would not require any back-references using the dreaded -E
flag that BSD sed
is so infamous for:
grep -oE 'SOFTWARE LICENSE AGREEMENT FOR OS X.*[A-Z]' '/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/en.lproj/OSXSoftwareLicense.rtf' | sed "s/SOFT.*OS X //"
Personally, I prefer the awk
method instead:
grep -oE 'SOFTWARE LICENSE AGREEMENT FOR OS X.*[A-Z]' '/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/en.lproj/OSXSoftwareLicense.rtf' | awk -F 'OS X ' '{print $NF}'
Pure awk
solution:
awk '/SOFTWARE LICENSE AGREEMENT FOR OS X/' '/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/en.lproj/OSXSoftwareLicense.rtf' | awk -F 'OS X ' '{print $NF}' | awk '{print substr($0, 0, length($0)-1)}'
(I'm sure there's a way to do it without piping to additional awk processes, but I'm not a pro.)