dbs9654
dbs9654

Reputation: 13

How to parse text blocks and get a value depending on another value in this block

Let's consider example output of vboxmanage list hdds:

UUID:           abc
Parent UUID:    base
State:          created
Type:           normal (base)
Location:       /home/me/VirtualBox VMs/not_me/b.vmdk
Storage format: VMDK
Capacity:       100000 MBytes
Encryption:     disabled

UUID:           def
Parent UUID:    base
State:          created
Type:           normal (base)
Location:       /home/me/VirtualBox VMs/my_file/a.vmdk
Storage format: VMDK
Capacity:       100000 MBytes
Encryption:     disabled

UUID:           ghi
Parent UUID:    base
State:          created
Type:           normal (base)
Location:       /home/me/VirtualBox VMs/my_file/a.vmdk
Storage format: VMDK
Capacity:       100000 MBytes
Encryption:     disabled

I would like to get output like:

def
ghi

In other words I need UUIDs of disks from /home/me/VirtualBox VMs/my_file and not UUID that belongs to /home/me/VirtualBox VMs/not_me/b.vmdk

Upvotes: 1

Views: 100

Answers (3)

Gilles Quénot
Gilles Quénot

Reputation: 185025

Using , with this, the order of lines can change, unlike other responses:

vboxmanage list hdds | perl -lne '
    BEGIN{ $/ = "\n\n" }
    print $1 if m/UUID:\s+(\w+)\s+.*my_file/s
'

Output

def
ghi

Upvotes: 0

MichalH
MichalH

Reputation: 1074

Using grep and sed:

vboxmanage list hdds | grep -B 4 '/home/me/VirtualBox VMs/my_file/' | 
sed -n 's/^UUID:\s*//p'

Upvotes: 1

Gilles Quénot
Gilles Quénot

Reputation: 185025

Using :

vboxmanage list hdds | awk '$1=="UUID:"{uid=$2} /^Location:.*my_file/{print uid}'

Output is

def
ghi

Upvotes: 0

Related Questions