Bash 用于快速查看 VM 管理程序 (KVM) 的脚本
Bash script for viewing VM quickly the hypervisor (KVM)
我正在寻找一种在寻找来宾虚拟机 (运行 KVM) 时快速查看管理程序的方法。
我有一个脚本将我所有的虚拟机管理程序(带有来宾虚拟机)收集到一个由换行符分隔的文本文件中(参见下面的示例):
Hypervisor: hypervisor1
ID Name State
----------------------------------------------------
1 vm1 running
2 vm2 running
3 vm3 running
4 vm4 running
Hypervisor: hypervisor2
ID Name State
----------------------------------------------------
1 vm1 running
2 vm2 running
3 vm3 running
4 vm4 running
5 vm5 running
6 vm6 running
ETC....
我试过:
grep -v -E "(-|Name)" file.txt |
awk -F ' ' '{print }' |
tr "\n" " " |
sed "s/ * / -> /"
但我明白了:
hypervisor1 -> vm1 vm2 vm3 vm4 hypervisor2 vm1 vm2 vm3 vm4 vm5 vm6
我的问题是:如何显示它?
hypervisor1 -> vm1 vm2 vm3 vm4
hypervisor2 -> vm1 vm2 vm3 vm4 vm5 vm6
etc.. etc...
根据您展示的示例,请您尝试以下操作。
awk '
/Hypervisor:/{
if(value1){
print value,value1
}
found=value1=""
value=$NF
next
}
/ID/{
found=1
next
}
found && match([=10=],/vm[0-9]+/){
value1=(value1?value1 OFS:"")substr([=10=],RSTART,RLENGTH)
}
END{
if(value1){
print value,value1
}
}
' Input_file
说明: 为以上添加详细说明。
awk ' ##Starting awk program from here.
/Hypervisor:/{ ##Checking condition if line contains Hypervisor: then do following.
if(value1){ ##Checking if value1 is NOT NULL.
print value,value1 ##Printing value and value1 here.
}
found=value1="" ##Nullify found and value1 here.
value=$NF ##Setting value as last field here.
next ##next will skip all further statements from here.
}
/ID/{ ##Checking condition if ID is found in current line then do following.
found=1 ##Setting found to 1 here.
next ##next will skip all further statements from here.
}
found && match([=11=],/vm[0-9]+/){ ##Checking if found is SET and match function to match vm values.
value1=(value1?value1 OFS:"")substr([=11=],RSTART,RLENGTH)
##Creating value1 which has matched sub string and keep adding its values to it.
}
END{ ##Starting END block of this program from here.
if(value1){ ##Checking condition if value1 is set then do following.
print value,value1 ##Printing value and value1 here.
}
}
' Input_file ##Mentioning Input_file name here.
使用 awk:
awk '/Hypervisor:/ {printf "%s ->", } # row contains Hypervisor:
[=10=]=="" {print ""} # row is empty
~/[0-9]/ {printf " %s", } # first column contains digit
END {print ""}' file # add a trailing newline
输出:
hypervisor1 -> vm1 vm2 vm3 vm4
hypervisor2 -> vm1 vm2 vm3 vm4 vm5 vm6
这可能对你有用 (GNU sed):
sed -E '/Hypervisor:/{s/.*: //;:a;x;/./{s/\n/ -> /;s// /g;p};x;h;d}
/^\s*[0-9]+\s*(\S+).*/{s///;H};$!d;ba' file
将相关详细信息存储在 hold space 中,并在管理程序或文件末尾发生更改时,将存储的值处理为所需格式。
我正在寻找一种在寻找来宾虚拟机 (运行 KVM) 时快速查看管理程序的方法。
我有一个脚本将我所有的虚拟机管理程序(带有来宾虚拟机)收集到一个由换行符分隔的文本文件中(参见下面的示例):
Hypervisor: hypervisor1
ID Name State
----------------------------------------------------
1 vm1 running
2 vm2 running
3 vm3 running
4 vm4 running
Hypervisor: hypervisor2
ID Name State
----------------------------------------------------
1 vm1 running
2 vm2 running
3 vm3 running
4 vm4 running
5 vm5 running
6 vm6 running
ETC....
我试过:
grep -v -E "(-|Name)" file.txt |
awk -F ' ' '{print }' |
tr "\n" " " |
sed "s/ * / -> /"
但我明白了:
hypervisor1 -> vm1 vm2 vm3 vm4 hypervisor2 vm1 vm2 vm3 vm4 vm5 vm6
我的问题是:如何显示它?
hypervisor1 -> vm1 vm2 vm3 vm4
hypervisor2 -> vm1 vm2 vm3 vm4 vm5 vm6
etc.. etc...
根据您展示的示例,请您尝试以下操作。
awk '
/Hypervisor:/{
if(value1){
print value,value1
}
found=value1=""
value=$NF
next
}
/ID/{
found=1
next
}
found && match([=10=],/vm[0-9]+/){
value1=(value1?value1 OFS:"")substr([=10=],RSTART,RLENGTH)
}
END{
if(value1){
print value,value1
}
}
' Input_file
说明: 为以上添加详细说明。
awk ' ##Starting awk program from here.
/Hypervisor:/{ ##Checking condition if line contains Hypervisor: then do following.
if(value1){ ##Checking if value1 is NOT NULL.
print value,value1 ##Printing value and value1 here.
}
found=value1="" ##Nullify found and value1 here.
value=$NF ##Setting value as last field here.
next ##next will skip all further statements from here.
}
/ID/{ ##Checking condition if ID is found in current line then do following.
found=1 ##Setting found to 1 here.
next ##next will skip all further statements from here.
}
found && match([=11=],/vm[0-9]+/){ ##Checking if found is SET and match function to match vm values.
value1=(value1?value1 OFS:"")substr([=11=],RSTART,RLENGTH)
##Creating value1 which has matched sub string and keep adding its values to it.
}
END{ ##Starting END block of this program from here.
if(value1){ ##Checking condition if value1 is set then do following.
print value,value1 ##Printing value and value1 here.
}
}
' Input_file ##Mentioning Input_file name here.
使用 awk:
awk '/Hypervisor:/ {printf "%s ->", } # row contains Hypervisor:
[=10=]=="" {print ""} # row is empty
~/[0-9]/ {printf " %s", } # first column contains digit
END {print ""}' file # add a trailing newline
输出:
hypervisor1 -> vm1 vm2 vm3 vm4 hypervisor2 -> vm1 vm2 vm3 vm4 vm5 vm6
这可能对你有用 (GNU sed):
sed -E '/Hypervisor:/{s/.*: //;:a;x;/./{s/\n/ -> /;s// /g;p};x;h;d}
/^\s*[0-9]+\s*(\S+).*/{s///;H};$!d;ba' file
将相关详细信息存储在 hold space 中,并在管理程序或文件末尾发生更改时,将存储的值处理为所需格式。