这是我的txt文件
type=0
vcpu_count=10
maste=0
h=0
p=0
memory=23.59
num=2
我想获取 vcpu_count 和内存值,并通过 perl(自动化脚本)将其存储在某个数组中。
awk -F'=' '/vcpu_count/{printf "\n",$1}' .vmConfig.txt
我使用此命令只是为了在终端上进行测试。但我得到的是一个空行。我该怎么做。我需要获取这两个值并检查条件
最佳答案
如果您无论如何都在使用 Perl,那么也只需为此使用 Perl。
my %array;
open ($config, "<", ".vmConfig.txt") or die "$0: Could not open .vmConfig.txt: $!\n";
while (<$config>) {
next unless /^\s*(vcpu_count|memory)\s*=\s*(.*?)\s*\n/;
$array{$1} = $2;
}
close($config);
如果您不希望结果是关联数组(又名哈希),重构应该相对容易。
关于linux - 如何使用awk进行过滤(perl自动化),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48704760/