从PowerShell中的文本文件中提取数据

时间:2022-12-08 09:47:13

I'm wondering if it is even possible to grab specific data from text file with format like below:

我想知道是否有可能从文本文件中获取特定数据,格式如下:

Adapter Number:         1
Adapter Name:           elxfc
                                Device 1
                                SCSI id:                1
                                LUN:                    0
                                Vendor:                 HP
                                Product:                Ultrium 4-SCSI
                                Firmware:               H6FW



Adapter Number:         2
Adapter Name:           elxfc
                                Device 1
                                SCSI id:                1
                                LUN:                    0
                                Vendor:                 HP
                                Product:                Ultrium 4-SCSI
                                Firmware:               H6FW


                                Changer 1
                                SCSI id:                1
                                LUN:                    1
                                Vendor:                 HP
                                Product:                MSL G3 Series
                                Firmware:               8.70

Form here at the end I woul like to get info about devices to get output like:

最后在这里形成我想获得有关设备的信息,以获得输出:

Ultrium 4-SCSI, 1 1 0
Ultrium 4-SCSI, 2 1 0
MSL G3 Series, 2 1 1

I've tried some basic string operations, but no luck. The problem is to catch second device within the same "main" Adapter Number.

我尝试了一些基本的字符串操作,但没有运气。问题是在同一“主”适配器号内捕获第二个设备。

1 个解决方案

#1


Something like:

switch -regex -file $inputFile {
  "Adapter Number:\s+(\d+)" { $adapterNumber = $Matches[1]; break; }
  "\s+SCSI id:\s+(\d+)" { $scsiId = $Matches[1]; break; }
  "\s+LUN:\s+(\d+)" { $lun = $Matches[1]; break; }
  "\d+Product:\s+(.+)" { 
                   $prod = $Matches[1];
                   "{0}, {1} {2} {3}" -f $prod, $adapterNumber, $scsiId, $lun;
                  }
}

should do it.

应该这样做。

With the -regex witch the matching is done with regexes, with -file each line of the passed file is processed through the switch (see online for more details on PSH's switch). Otherwise it is just keeping the state needed for output when Product: is matched.

使用-regex女巫,匹配是使用正则表达式完成的,使用-file通过交换机处理传递文件的每一行(有关PSH交换机的详细信息,请参阅在线)。否则,它只是保持Product:匹配时输出所需的状态。

#1


Something like:

switch -regex -file $inputFile {
  "Adapter Number:\s+(\d+)" { $adapterNumber = $Matches[1]; break; }
  "\s+SCSI id:\s+(\d+)" { $scsiId = $Matches[1]; break; }
  "\s+LUN:\s+(\d+)" { $lun = $Matches[1]; break; }
  "\d+Product:\s+(.+)" { 
                   $prod = $Matches[1];
                   "{0}, {1} {2} {3}" -f $prod, $adapterNumber, $scsiId, $lun;
                  }
}

should do it.

应该这样做。

With the -regex witch the matching is done with regexes, with -file each line of the passed file is processed through the switch (see online for more details on PSH's switch). Otherwise it is just keeping the state needed for output when Product: is matched.

使用-regex女巫,匹配是使用正则表达式完成的,使用-file通过交换机处理传递文件的每一行(有关PSH交换机的详细信息,请参阅在线)。否则,它只是保持Product:匹配时输出所需的状态。