简体   繁体   English

如何从选择字符串获取值

[英]How to get a value from Select-String

I have several files in a folder, those are .xml files. 我的文件夹中有几个文件,它们是.xml文件。

I want to get a value from those files. 我想从这些文件中获取价值。 A line in the file, could look like this: 文件中的一行可能看起来像这样:

<drives name="Virtual HD ATA Device" deviceid="\\.\PHYSICALDRIVE0" interface="IDE" totaldisksize="49,99">

What i'm trying to do is get the value 49,99 in this case. 在这种情况下,我要尝试获取的值是49,99。

I am able to get the line out of the file with: 我可以通过以下方式将行从文件中删除:

$Strings = Select-String -Path "XML\*.xml" -Pattern totaldisksize

foreach ($String in $Strings) {
        Write-Host "Line is" $String

}

But getting just the value in "" i don't get how. 但是仅仅获得“”中的值,我不知道如何获得。 I've also played around with 我也玩过

$Strings.totaldisksize

But no dice. 但是没有骰子。

Thanks in advance. 提前致谢。

You can do this in one line as follows: 您可以在一行中执行以下操作:

$(select-string totaldisksize .\XML\*.xml).line -replace '.*totaldisksize="(\d+,\d+)".*','$1'

The Select-String will give you a collection of objects that contains information about the match. Select-String将为您提供对象集合,其中包含有关匹配的信息。 The line property is the one you're interested in, so you can pull that directly. line属性是您感兴趣的属性,因此您可以直接将其拉出。

Using the -replace operator, every time the .line property is a match of totaldisksize , you can run the regex on it. 使用-replace运算符,每当.line属性与totaldisksize匹配时,就可以在其上运行正则表达式。 The $1 replacement will grab the group in the regex, the group being the part in parentheses (\\d+,\\d+) which will match one or more digits, followed by a comma, followed by one or more digits. $1替换将抓住正则表达式中的组,该组是括号(\\d+,\\d+) ,它将匹配一个或多个数字,后跟一个逗号,然后是一个或多个数字。

This will print to screen because by default powershell will print an object to the screen. 这将打印到屏幕上,因为默认情况下Powershell会将对象打印到屏幕上。 Because you're only accessing the .line property, that's the only bit that's printed and also only after the replacement has been run. 因为您仅访问.line属性,所以这是唯一打印的内容,并且仅执行替换操作之后才打印。

If you wanted to explicitly use a Write-Host to see the results, or do anything else with them, you could store to a variable as follows: 如果要显式使用Write-Host来查看结果,或对结果进行任何其他操作,则可以将变量存储如下:

$sizes = $(select-string totaldisksize .\XML\*.xml).line -replace '.*totaldisksize="(\d+,\d+)".*','$1'
$sizes | % { Write-Host $_ }

The above stores the results to an array, $sizes , and you iterate over it by piping it to the Foreach-Object or % . 上面的代码将结果存储到$sizes数组中,然后通过将其通过管道传递到Foreach-Object%对其进行迭代。 You can then access the array elements with $_ inside the block. 然后,您可以在块内使用$_访问数组元素。

I am not sure about powershell but if you prefer using python below is the way of doing it. 我不确定powershell但是如果您更喜欢使用python则可以使用它。

import re

data = open('file').read()
item = re.findall('.*totaldisksize="([\d,]+)">', data)
print(item[0])

Output 产量

49,99

But.. but.. PowerShell knows XML. 但是.... PowerShell知道XML。

$XMLfile = '<drives name="Virtual HD ATA Device" deviceid="\\.\PHYSICALDRIVE0" interface="IDE" totaldisksize="49,99"></drives>'
$XMLobject = [xml]$XMLfile
$XMLobject.drives.totaldisksize

Output 产量

49,99

Or walk the tree and return the content of "drives": 或走到树上并返回“驱动器”的内容:

$XMLfile = @"
<some>
  <nested>
     <tags>       
        <drives someOther="stuff" totaldisksize="49,99" freespace="22,33">
        </drives>
     </tags>
  </nested>
</some>
"@

$drives = [xml]$XMLfile | Select-Xml -XPath "//drives" | select -ExpandProperty node

Output 产量

PS> $drives

someOther         totaldisksize           freespace
---------         -------------           ---------
stuff                     49,99               22,33

PS> $drives.freespace
22,33

XPath query of "//drives" = Find all nodes named "drives" anywhere in the XML tree. XPath查询“ // drives” =在XML树的任何位置查找所有名为“ drives”的节点。 Reference: Windows PowerShell Cookbook 3rd Edition (Lee Holmes). 参考:Windows PowerShell食谱第三版(Lee Holmes)。 Page 930. 第930页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM