从输出构建数组

发布于 2024-10-02 01:36:52 字数 834 浏览 3 评论 0原文

我正在尝试创建一个脚本来获取系统命令的输出,并且我想将数据组织到一个数组中。

该脚本用于发现驻留在我拥有的本地 ESXi 服务器上的虚拟机。我使用 plink.exe 将命令发送到服务器,然后它返回如下所示的虚拟机列表。

Vmid   Name                File                 Guest OS      Version   Annotation
128    NS01    [datastore2] NS01/NS01.vmx     ubuntu64Guest   vmx-07
144    NS02    [datastore2] NS02/NS02.vmx     ubuntu64Guest   vmx-07
208    MX01    [datastore2] MX01/MX01.vmx     ubuntu64Guest   vmx-07
224    SQL01   [datastore2] SQL01/SQL01.vmx   ubuntu64Guest   vmx-07
240    WS01    [datastore2] WS01/WS01.vmx     ubuntu64Guest   vmx-07

我将如何利用它并用它制作一个数组?唯一真正重要的列是 VMID、名称、文件

我用来获取输出的命令是这样的。

# Parse ESX\ESXi server for virtual machines that reside on it
system ("$plink \-batch \-pw $esx_password $esx_user\@$esx_host vim-cmdvmsvc/getallvms\n");

任何见解都会很棒。

I am trying to create a script that will take output of a system command and I want to organize the data into an array.

The script is to discover VMs residing on a local ESXi server I have. I am using plink.exe to send the command to the server and then it returns a list of VMs that looks like this.

Vmid   Name                File                 Guest OS      Version   Annotation
128    NS01    [datastore2] NS01/NS01.vmx     ubuntu64Guest   vmx-07
144    NS02    [datastore2] NS02/NS02.vmx     ubuntu64Guest   vmx-07
208    MX01    [datastore2] MX01/MX01.vmx     ubuntu64Guest   vmx-07
224    SQL01   [datastore2] SQL01/SQL01.vmx   ubuntu64Guest   vmx-07
240    WS01    [datastore2] WS01/WS01.vmx     ubuntu64Guest   vmx-07

How would I take this and make an array out of it? The only columns that really matter are VMID, Name, File

The command I am using to get the output is this.

# Parse ESX\ESXi server for virtual machines that reside on it
system ("$plink \-batch \-pw $esx_password $esx_user\@$esx_host vim-cmdvmsvc/getallvms\n");

Any insight would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖公主 2024-10-09 01:36:52

是的,如果您想解析输出,请使用反引号而不是系统

my $cmd 
    = "$plink -batch -pw $esx_password $esx_user\@$esx_host "
    . "vim-cmdvmsvc/getallvms"
    ;
my @lines = `$cmd`;

system 将仅使用您的标准输出。

不过,不确定为什么要转义破折号...

一旦获得输出,您可以执行以下操作(请注意,我利用了固定长度字段的外观):

foreach ( @line ) { 
    # this is the get-it-and-do-something-else-with-it version
    my ( $vmid, $name, $file )
        = substr( $_, 0, 47 ) =~ m/^ ( \d+ ) \s+ ( \S+ ) \s+ (.*\S) \s* $/x
        ;

    # OR the store-it-in-an-array-of-hashes version:
    @{ my $h = {}}{ qw<VMID Name File> }
        = substr( $_, 0, 47 ) =~ m/^ ( \d+ ) \s+ ( \S+ ) \s+ (.*\S) \s* $/x
        ;
    push @array, $h if %$h;
}

Yes, use backquotes not system if you want to parse the output.

my $cmd 
    = "$plink -batch -pw $esx_password $esx_user\@$esx_host "
    . "vim-cmdvmsvc/getallvms"
    ;
my @lines = `$cmd`;

system will just use your standard out.

Not sure why you were escaping the dashes, though...

Once you have the output, you can do the following (note that I leveraged the fixed-length fields look):

foreach ( @line ) { 
    # this is the get-it-and-do-something-else-with-it version
    my ( $vmid, $name, $file )
        = substr( $_, 0, 47 ) =~ m/^ ( \d+ ) \s+ ( \S+ ) \s+ (.*\S) \s* $/x
        ;

    # OR the store-it-in-an-array-of-hashes version:
    @{ my $h = {}}{ qw<VMID Name File> }
        = substr( $_, 0, 47 ) =~ m/^ ( \d+ ) \s+ ( \S+ ) \s+ (.*\S) \s* $/x
        ;
    push @array, $h if %$h;
}
依 靠 2024-10-09 01:36:52

如果没有数据列包含空格,您可以轻松地将输出split(/\n/)分成几行,迭代这些行,然后split(/\s+/) > 将行放入数组中。然后按索引提取您感兴趣的列。

If none of the data columns will contain spaces, you could easily split(/\n/) the output into lines, iterate over those, and split(/\s+/) the rows into arrays. Then extract the columns you are interested in, by index.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文