I'm attempting to write a bash function that gets the UUID of a VirtualBox VM. I'm pretty new to awk
so I'm trying to focus on learning how to solve the problem using it. I'm aware that I can use sed
or even cut
to solve this.
My "raw" output from the VBoxManage list vms
is as follows:
$ VBoxManage list vms
"FreeBSD" {1aac7062-bd59-47ee-9261-2f6aa8d9ef53}
"Windows 10" {64942de7-beb9-418c-9f52-5befcb6f577b}
"High Sierra" {07f73e1a-a0c4-4190-ade1-79a2e432b4d6}
"Test Machine" {9d0953a7-ca2a-4667-8c5b-1a9f550b2956}
My desired output is to just get the UUID of a particular VM. Using "Test Machine" for this case, I'm looking for 9d0953a7-ca2a-4667-8c5b-1a9f550b2956
(without the brackets {
and }
).
After quite a bit of searching and testing, I've come up with
$ VBoxManage list vms | awk '/Test Machine/{ sub("{" ,""); sub("}", ""); print $3 }'
9d0953a7-ca2a-4667-8c5b-1a9f550b2956
It works, but I have to use to sub
commands to extract it.
My question is, is there a way to simplify the substitution portion of the action with an or
type operator so I don't have to use two sub
commands?
For example, if I try awk '/Test Machine/{ sub("{" || "}", ""); print $3'
it doesn't work - it prints the whole field including the brackets.
{9d0953a7-ca2a-4667-8c5b-1a9f550b2956}
Is there a better way of extracting that string?