在没有完整 Get-Content 的情况下提取早期属性值 [英] Extract early attribute values without full Get-Content

查看:17
本文介绍了在没有完整 Get-Content 的情况下提取早期属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 PowerShell 中,我知道我可以做这样的事情:

In PowerShell I know I can do something like this:

[xml]$myxml = Get-Contents .\oneofmyxmlfiles.xml
$myxml.Wrapper.Data.HeaderStuff.SomeHeaderAttribute

并从类似的东西中得到我想要的结果 (WhatIWantToExtract)

and get the result I want (WhatIWantToExtract) from something like

<Wrapper>
    <Data>
        <HeaderStuff SomeHeaderAttribute="WhatIWantToExtract" />
...a bunch of other stuff...
    </Data>
...a bunch of more other stuff...
</Wrapper>

我需要为许多文件执行此操作,这些文件可能相当大.但是,HeaderStuff 元素唯一,并且总是非常靠近文件顶部.

I need to do this for many files which can be quite large. But, the HeaderStuff element is unique and always very near the top of the file.

有没有办法从文件顶部附近直接"提取,而无需使用 Get-Content 加载整个内容?

Is there a way to extract "directly" from near the top the file, without loading the whole contents with Get-Content?

推荐答案

我将使用 TheIncorrigible1 建议,使用 Get-Content 的 TotalCount 参数":

I will use TheIncorrigible1 suggestion, "use TotalCount parameter of Get-Content":

$fivelines = Get-Content .\oneofmyxmlfiles.xml -TotalCount 5

结果是具有五个元素的 System.Array.我的 XML 文件具有非常可预测的结构,因此我可以轻松地隔离我需要的特定行(数组元素),然后使用简单的字符串匹配技术来获取我所追求的属性值.

The result is a System.Array with five elements. My XML files have a very predictable structure, so I can easily isolate the specific line (array element) I need, then use simple string matching techniques to get out the attribute value I am after.

感谢 Tomalak,您的解决方案看起来更优雅、更可靠,但目前我愿意做的工作比我多.

Thanks to Tomalak, your solution seems more elegant and robust, but more work than I am willing to do at this moment.

这篇关于在没有完整 Get-Content 的情况下提取早期属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆