如何读取.txt文件中的文本及其在powershell中的每个值? [英] How to read text in .txt file and each of its value in powershell?
问题描述
我在如下所示的文本文件中有数据,我想使用powershell逐个读取值
I have data in a text file like below, i want to read it value by value using 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
例如,我想在第二个字段中读取名称的值.怎么读?
For example i want to read value for name in 2nd field. How can it be read?
推荐答案
It is difficult to convert it without having a delimiter in place. With a delimiter you could make use of ConvertFrom-String
如果我们以这种格式使用ConvertFrom-String,它将正确解析前两列,但是描述将用单词分隔.
If we would use ConvertFrom-String with this format, it will parse the first 2 columns correctly, but the description will be seperated in words.
((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
要获得第二个结果,必须考虑标题和分隔符行. 这将返回完整的对象.
To get the second result you have to take in account the header and separator line. This will return the full object.
((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
这只会返回已启用"中的值
This will only return the value from "Enabled"
(((Get-Content .\test.txt) | ConvertFrom-String -PropertyNames Name, Enabled)[3]).Enabled
结果:
True
这篇关于如何读取.txt文件中的文本及其在powershell中的每个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!