gpt4 book ai didi

windows - 如何读取 .txt 文件中的文本及其在 powershell 中的每个值?

转载 作者:可可西里 更新时间:2023-11-01 11:23:12 27 4
gpt4 key购买 nike

我在如下文本文件中有数据,我想使用 powershell 按值读取它

Name           Enabled Description                                             
---- ------- -----------
administrator1 True
Azureuser True Built-in account for administering the computer/domain
DefaultAccount False A user account managed by the system.
Guest False Built-in account for guest access to the computer/domain

例如,我想读取第二个字段中名称的值。怎么读?

最佳答案

如果没有定界符就很难转换它。使用分隔符,您可以使用 ConvertFrom-String

如果我们使用这种格式的 ConvertFrom-String,它将正确解析前 2 列,但描述将以单词分隔。

((Get-Content .\test.txt) | ConvertFrom-String -PropertyNames Name, Enabled)

结果:

Name           Enabled P3          P4
---- ------- -- --
Name Enabled Description
---- ------- -----------
administrator1 True
Azureuser True Built-in account
DefaultAccount False A user
Guest False Built-in account

要获得第二个结果,您必须考虑标题和分隔线。这将返回完整的对象。

((Get-Content .\test.txt) | ConvertFrom-String -PropertyNames Name, Enabled)[3]

结果:

Name    : Azureuser
Enabled : True
P3 : Built-in
P4 : account
P5 : for
P6 : administering
P7 : the
P8 : computer/domain

这只会返回“已启用”的值

(((Get-Content .\test.txt) | ConvertFrom-String -PropertyNames Name, Enabled)[3]).Enabled

结果:

True

关于windows - 如何读取 .txt 文件中的文本及其在 powershell 中的每个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55489841/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com