gpt4 book ai didi

awk - 如何使用 awk 提取特定 VM 和组的 UUID?

转载 作者:行者123 更新时间:2023-12-04 03:23:24 24 4
gpt4 key购买 nike

我正在使用 vboxmanage list -l vms,我想通过提供 NameGroups 值来提取特定机器的 UUID .

Name:                        vm2
Groups: /C05Lab1
Guest OS: Oracle (64-bit)
UUID: 54c233e8-3d5b-46bb-9a21-a8946ffa7d76
Config file: C:\Users\Bianca\VirtualBox VMs\C05Lab1\vm2\vm2.vbox
Snapshot folder: C:\Users\Bianca\VirtualBox VMs\C05Lab1\vm2\Snapshots
Log folder: C:\Users\Bianca\VirtualBox VMs\C05Lab1\vm2\Logs
Hardware UUID: 54c233e8-3d5b-46bb-9a21-a8946ffa7d76

我必须:

vboxmanage list -l vms | awk '$1=="UUID:"{uid=$2} /Name: *vm2/{print uid}'

它至少列出了一个 uuid,但不是正确的(它在它之前打印机器的 uuid)而且我不知道如何检查 Groups 值,因为它以/

最佳答案

使用您展示的示例,请尝试遵循 awk 代码。

vboxmanage list -l vms | 
awk -v name="vm2" -v group="C05Lab1" '
/^Name:/{
foundName=foundGroup=""
if($2==name){ foundName=1 }
next
}
/^Groups:/ && $2==group{
foundGroup=1
next
}
/^UUID:/ && foundName && foundGroup{
print
foundName=foundGroup=""
}'

说明:在此处添加详细说明。

vboxmanage list -l vms |               ##Running command vboxmanage list -l vms and sending its Output to awk as an input.
awk -v name="vm2" -v group="C05Lab1" ' ##Starting awk command from here and setting name to vm2 and group to C05Lab1 here.
/^Name:/{ ##Checking condition if line starts with Name: then do following.
foundName=foundGroup="" ##Nullifying foundName and foundGroup here.
if($2==name){ foundName=1 } ##Checking condition if 2nd field is name then set foundName to 1 here.
next ##next will skip all further statements from here.
}
/^Groups:/ && $2==group{ ##Checking condition if line starts from Groups: and 2nd field is group then do following.
foundGroup=1 ##Setting foundGroup to 1 here.
next ##next will skip all further statements from here.
}
/^UUID:/ && foundName && foundGroup{ ##Checking condition if line starts from UUID: and foundName and foundGroup are set then do following.
print ##Printing currnet line here.
foundName=foundGroup="" ##Nullifying foundName and foundGroup here.
}'

由 Ed Morton 编辑以展示注释代码的另一种方法,即注释说为什么我们正在做某事而不是什么我们正在做:

vboxmanage list -l vms |
awk -v name="vm2" -v group="C05Lab1" ' # Set variables to contain the Name: and Groups: values we want to find in the input
/^Name:/{ # We assume that every record starts with Name: and so we clear the
foundName=foundGroup="" # foundName and foundGroup flags here so that when we hit the UUID:
# line later they arent still set from reading the previous record.
if($2==name){ foundName=1 } # If the current value of the Name: matches the target value stored in
# our "name" variable then set the flag to indicate that so when we test
# it on our UUID: line we know we found the target name.
next
}
/^Groups:/ && $2==group{ # If the current value of the Group: matches the target value stored in
foundGroup=1 # our "group" variable then set the flag to indicate that so when we test
# it on our UUID: line we know we found the target group.
next
}
/^UUID:/ && foundName && foundGroup{ # Were on the UUID: line from the input so if we had previously found
print # our target "name" and "group" as indicated by the 2 flags we set earlier
# then we output the current line which contains the UUID.
foundName=foundGroup="" # Clear foundName and foundGroup flags here to ensure that when we hit the UUID:
# line for the next record they arent still set from reading the current record
}'

关于awk - 如何使用 awk 提取特定 VM 和组的 UUID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68095851/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com