这是grep -E“ scsi0:” * .vmx的输出。我的目录之一中的grep -E“ fileName”命令:
scsi0:0.fileName = "vmname.vmdk"
scsi0:1.fileName = "vmname_1.vmdk"
scsi0:2.fileName = "vmname_2.vmdk"
scsi0:3.fileName = "P120_vmname_2.vmdk"
scsi0:4.fileName = "P120_vmname_3.vmdk"
我需要在该vmx文件中重写以上输出,以便使其看起来像下面的
scsi0:0.fileName = "vmname.vmdk"
scsi0:1.fileName = "vmname_1.vmdk"
scsi0:2.fileName = "vmname_2.vmdk"
scsi0:3.fileName = "vmname_3.vmdk"
scsi0:4.fileName = "vmname_4.vmdk"
因此,实质上,脚本需要满足以下条件:
问题在于, vmname 之前可以有任意数量的字符需要删除,而需要修复的行可以位于源文件中的任何位置。
我需要将单独的行输出分配给单独的变量,还是可以只用一个变量来操纵它们?
谢谢
答案 0 :(得分:0)
您可以将grep
的输出通过管道传输到sed
吗?
如果是这样,则可以这样替换:
sed -r 's/^(.*:)([[:digit:]]+)(.*) = \".*vmname_[[:digit:]]+/\1\2\3 = \"vmname_\2/'
# ^ ^ ^ ^ ^ ^ ^
# | | | | | | Replace number.
# | | | | | Re-create line start
# | | | | Match incorrect number
# | | | Match garbage before "vmname"
# | | Match Filename
# | Correct number
# Line start
因此您的实际命令将如下所示:
grep -E "scsi0:" *.vmx | grep -E "fileName" | \
sed -r 's/^(.*:)([[:digit:]]+)(.*) = \".*vmname_[[:digit:]]+/\1\2\3 = \"vmname_\2/'
将其转换为
scsi0:0.fileName = "vmname.vmdk"
scsi0:1.fileName = "vmname_1.vmdk"
scsi0:2.fileName = "vmname_2.vmdk"
scsi0:3.fileName = "P120_vmname_2.vmdk"
scsi0:4.fileName = "P120_vmname_3.vmdk"
scsi0:5.fileName = "P12asdasdsada_asdasd_sdsad0_vmname_3.vmdk"
scsi0:6.fileName = "vmname_3.vmdk"
对此:
scsi0:0.fileName = "vmname.vmdk"
scsi0:1.fileName = "vmname_1.vmdk"
scsi0:2.fileName = "vmname_2.vmdk"
scsi0:3.fileName = "vmname_3.vmdk"
scsi0:4.fileName = "vmname_4.vmdk"
scsi0:5.fileName = "vmname_5.vmdk"
scsi0:6.fileName = "vmname_6.vmdk"