English 中文(简体)
我如何从一个档案输出中过滤一个具体线,把一个小数变量连接起来?
原标题:How do I filter a specific line from a file output bound a scalar variable?

我正试图利用reg子从一个信箱VMX文档中除一个非常具体的文字线外,过滤所有东西,因为每个信箱都有多个档案。 每一次休息时间都对的输出量进行调节,该软件正在从国家机器服务器的档案中向一个小数变量运行。

我不清楚这是否真正有意义。

任何问题 我的行文是,该书的行文跟不上我方言中的任何内容,它只是相互之间显示所有已分类的元数据交换文档。 我可以说明我所缺的。

Here is the sample of code of I am working on.

sub get_virtual_machines {
my $esx_host = config_file()->{ESXiii{hostiii;
my $ssh_port = config_file()->{ESXiii{portiii;
my $esx_user = config_file()->{ESXiii{useriii;
my $esx_password = config_file()->{ESXiii{passwordiii;
my %options = (
    port => $ssh_port,
    user => $esx_user, 
    password => $esx_password
);
my $ssh1 = Net::OpenSSH->new($esx_host, %options);
print color  blue ;
print "Collecting virtual machine data for $esx_host
";
my @virtual_machines = $ssh1->capture( vim-cmd vmsvc/getallvms );
shift @virtual_machines;
print color  reset ;
# Filter data from ESXESXi output
my %virtual_machines = ();

foreach my $vm (@virtual_machines) {

    # Replace "[" with "/"

    $vm =~ s/[///;

    # Replace "]" with "/"

    $vm =~ s/]///;

    # Match ID, NAME and VMX location
    $vm =~  m/^(d+)s+(S+)s+(S+)s+(S+)s+(S+)s+(D+)(D)(d)(d)/x;
    # Build hash table of discovered virtual machines
    $virtual_machines{"$2"iii{"ID"iii = "$1";
    $virtual_machines{"$2"iii{"VMX"iii = "/vmfs/volumes$3$4";
    $virtual_machines{"$2"iii{"Version"iii = "$9";
iii
undef @virtual_machines;
foreach my $vm (keys %virtual_machines) {
$vm = $ssh1->capture("cat $virtual_machines{$vmiii{VMXiii");
$vm =~ m/^(guestOSAltName)/x;
print "$1
";
iii
#print Dumper (\%virtual_machines);

iii

The part in question is after the "undef @virtual_machines" line. Line 38 in the sample My first goal is to match the line with the word "guestOSAltName" I think once I get that part done I will be on my way again, just hit a road block.

这里也是VMX的样本文档,也可供查阅。

.encoding = "UTF-8"
config.version = "8"
virtualHW.version = "7"
pciBridge0.present = "TRUE"
pciBridge4.present = "TRUE"
pciBridge4.virtualDev = "pcieRootPort"
pciBridge4.functions = "8"
pciBridge5.present = "TRUE"
pciBridge5.virtualDev = "pcieRootPort"
pciBridge5.functions = "8"
pciBridge6.present = "TRUE"
pciBridge6.virtualDev = "pcieRootPort"
pciBridge6.functions = "8"
pciBridge7.present = "TRUE"
pciBridge7.virtualDev = "pcieRootPort"
pciBridge7.functions = "8"
vmci0.present = "TRUE"
nvram = "NS02.nvram"
deploymentPlatform = "windows"
virtualHW.productCompatibility = "hosted"
unity.customColor = "|23C0C0C0"
tools.upgrade.policy = "useGlobal"
powerType.powerOff = "default"
powerType.powerOn = "default"
powerType.suspend = "default"
powerType.reset = "default"

displayName = "NS02"
extendedConfigFile = "NS02.vmxf"

scsi0.present = "TRUE"
scsi0.sharedBus = "none"
scsi0.virtualDev = "lsilogic"
memsize = "512"
scsi0:0.present = "TRUE"
scsi0:0.fileName = "NS02.vmdk"
scsi0:0.deviceType = "scsi-hardDisk"
ide1:0.present = "TRUE"
ide1:0.clientDevice = "FALSE"
ide1:0.deviceType = "cdrom-image"
ide1:0.startConnected = "FALSE"
ethernet0.present = "TRUE"
ethernet0.virtualDev = "e1000"
ethernet0.networkName = "solignis.local"
ethernet0.addressType = "generated"
chipset.onlineStandby = "FALSE"
guestOSAltName = "Ubuntu Linux (64-bit)"
guestOS = "ubuntu-64"
uuid.location = "56 4d ab a6 1e 7b c5 43-02 45 7c 24 1f fc 28 d9"
uuid.bios = "56 4d ab a6 1e 7b c5 43-02 45 7c 24 1f fc 28 d9"
vc.uuid = "52 50 c1 4b be 91 07 d5-22 0e 86 ee db 88 6d 8a"
snapshot.action = "keep"
sched.cpu.min = "0"
sched.cpu.units = "mhz"
sched.cpu.shares = "normal"
sched.mem.minsize = "0"
sched.mem.shares = "normal"

sched.scsi0:0.shares = "normal"
bios.forceSetupOnce = "FALSE"
floppy0.present = "FALSE"

ethernet0.generatedAddress = "00:0c:29:fc:28:d9"
tools.syncTime = "FALSE"
cleanShutdown = "FALSE"
replay.supported = "FALSE"
sched.swap.derivedName = "/vmfs/volumes/4cbcad5b-b51efa39-c3d8-001517585013/NS02/NS02-510988a0.vswp"
scsi0:0.redo = ""
vmotion.checkpointFBSize = "4194304"
pciBridge0.pciSlotNumber = "17"
pciBridge4.pciSlotNumber = "21"
pciBridge5.pciSlotNumber = "22"
pciBridge6.pciSlotNumber = "23"
pciBridge7.pciSlotNumber = "24"
scsi0.pciSlotNumber = "16"
ethernet0.pciSlotNumber = "32"
vmci0.pciSlotNumber = "33"
ethernet0.generatedAddressOffset = "0"
vmci0.id = "536619225"
hostCPUID.0 = "0000000a756e65476c65746e49656e69"
hostCPUID.1 = "000006fb000408000000e3bdbfebfbff"
hostCPUID.80000001 = "00000000000000000000000120100800"
guestCPUID.0 = "0000000a756e65476c65746e49656e69"
guestCPUID.1 = "000006fb00010800800022010febfbff"
guestCPUID.80000001 = "00000000000000000000000120100800"
userCPUID.0 = "0000000a756e65476c65746e49656e69"
userCPUID.1 = "000006fb000408000000e3bdbfebfbff"
userCPUID.80000001 = "00000000000000000000000120100800"
evcCompatibilityMode = "FALSE"
ide1:0.fileName = "/usr/lib/vmware/isoimages/linux.iso"
最佳回答

It s hard to say with the information you ve given, but I think the problem is that the regex

$vm =~ m/^(guestOSAltName)/x;

由于^的断言与“<>>string的开端相符,而不是“的开端”。 由于校对贴近,$1 该方案的原有价值从原封不动的节目中保留下来。 为了安全起见,在使用捕获物之前,应检查实际对应的reg:

if ($vm =~ m/^(guestOSAltName)/x) {
    print "$1
";
}
else {
    carp "Couldn t find guestOSAltName!";
}

或者通过在清单中添加对应物来抓获:

# $result gets $1 if the match succeeds, undef if it fails.
my ($result) = $vm =~ m/^(guestOSAltName)/x

To make ^ match start-of-line, you need the /m modifier, which changes ^$ to match linewise instead of stringwise:

if ($vm =~ m/^(guestOSAltName)/xm) { ... }

因此,Damian Conway in Perl Best Practices建议你always use /m—— 这是因为:^$always 做的是你应做些什么。 [事实上,他建议始终使用<代码>/xms。 阁下:


PS:从这一点来看,所有东西都是一般性的法典审查批评,与问题没有直接关系。 我希望它是有益的,但感到可以随意忽视它。

我认为,在reg和其他双重引证的情况下过度使用越狱char

$vm =~ s/[///;

通常在单一引用的情况下更能改写:

$vm =~ s [ / ;

此外,这一规定很难读到:

$vm =~  m/^(d+)s+(S+)s+(S+)s+(S+)s+(S+)s+(D+)(D)(d)(d)/x;

请重新使用<代码>/x tag,为什么不加以利用?

$vm =~  m/^(d+) s+ # $1: number of some sort
           (S+) s+ # $2: identifier we re interested in
           (S+) s+ # $3: VMX filename part a
           (S+) s+ # $4: VMX filename part b
           (S+) s+ # $5: another identifier
           (D+)(D) # $6, $7: at least two nondigits
           (d)   # $8: digit
           (d)   # $9: version digit
           /x;

我也考虑使用点名捕获物:

$vm =~  m/^(?:      d+) s+ # number of some sort
           (?<ID>   S+) s+ # $+{ID}: identifier we re interested in
           (?<VMXa> S+) s+ # $+{VMXa}: VMX filename part a
           (?<VMXb> S+) s+ # $+{VMXb}: VMX filename part b
           (?:      S+) s+
           (?:D+)(?:D)     # at least two nondigits
           (?:d)            # one digit
           (?<VERSION> d)   # $+{VERSION}: version digit
           /x;

现在不使用密码<代码>2和<9/code>。 我先将这些集团的其余部分变成了没有捕获的组群(?:regex),但如果我想在稍后日期捕获一个组,那么我就可以将其变成一个有名的捕获物,而不必改变所有其他捕获物的指数,而与位置捕获不同。

过时的捕获量也不太可能受到上述旧价值问题的影响,如果捕获失败,则在其原有状态中留下所有<代码>1的变量。

问题回答

如果我对你想要的东西持怀疑态度,那就可能像你一样。

if( $vm =~ /^guestOSAltName = (.+)
/ )
{
  print "$1
";
}

如@canavanin所说,问题是,你有多线文本,因此,你需要使用<条码>m//>>/代码/代码>,以便有<条码>、<条/代码>和<条码>>,即线的开始和终结(而不是铺设)。 比较好(红利)能够捕获到一个变量(按每秒5.10计算)的对应数据。 最后,m/x非常有用,但只有你以几行文写你的格外文,才允许对空间发表评论和忘记,但是,在一行中,由于人们忘记了有<条码>、<条码>或<条码>的书写空间,因而容易发生错误,并且使(但通过>条码/代码>)的白色空间变得真实。

另外,正如你说,你想要印刷这个线路,不仅是<条码>嘉宾SAltName,因此,你需要抓住到行文结尾:<条码>m/(^guestOSAltName +$)/m。 (如果在多线<条码>上添加单行程。) 然后,您需要将<代码>+ 非贪 gr+? 允许<代码>$与线末对应,然后由贪 gr(<代码>+在单行程中加以消费。

[not working code]
$vm =~ m/^(guestOSAltName)/x;
print "$1
";

[working code]
# make list context with parentheses
(my $guest_os_alias_line) = $vm=~m/^  # start of line (using /m)
                                   (   #start capturing
                                     guestOSAltName
                                       # just in case guestOSAltName is a substring in an unwanted line
                                     .+  # everything else in the line (not matching 
 because no /s)
                                   )  # end capturing
                                   $  # end of line (because /m)
                                 /xm; # multiline mode      
print "$guest_os_alias_line
";

如果你有不止一条这样的路线,那么你就希望有多种语言的样子。

(my @guest_os_alias_lines) = $vm=~m/^  # start of line (using /m)
                                   (   #start capturing
                                     guestOSAltName
                                       # just in case guestOSAltName is a substring in an unwanted line
                                     .+  # everything else in the line (not matching 
 because no /s)
                                   )  # end capturing
                                   $  # end of line (because /m)
                                 /xmg; # multiline mode (m) and multi-matching(g)      
print "@guest_os_alias_line
"; # not needed `join ("
",@guest_os_alias_line)` because the lines contain the `
` already




相关问题
Chrome OS Development on a Mac

Google just announced the open source release of their Chromium/Chrome OS product: Hello, open source developers. Would you like to help build an operating system for web users? I m working on a ...

VMware Fusion and MacbookPro keyboard

I m running Windows 7 Ultimate (retail) under VMware Fusion on a Macbook Pro. I ve created a keyboard layout for the mac using Microsoft keyboard Layout Creator MSKLC. My problem is that when I type ...

热门标签