Windows 如何读取.txt文件中的文本及其在powershell中的每个值?
我有一个文本文件中的数据,如下所示,我想使用powershell逐值读取它Windows 如何读取.txt文件中的文本及其在powershell中的每个值?,windows,powershell,azure-powershell,Windows,Powershell,Azure Powershell,我有一个文本文件中的数据,如下所示,我想使用powershell逐值读取它 Name Enabled Description ---- ------- ----------- administrator1 True
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
例如,我想读取第二个字段中名称的值。怎么读呢
例如,我想读取第二个字段中名称的值。怎么读呢
如果我没有误解,您想读取txt文件中的Azureuser,请尝试下面的命令,$txt[3]。拆分[0]就是您想要的
$txt = Get-Content 'C:\Users\joyw\Desktop\users.txt'
$txt[3].Split()[0]
在没有定界符的情况下很难转换它。使用分隔符,您可以使用 如果我们将ConvertFrom字符串与此格式一起使用,它将正确解析前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
Name : Azureuser
Enabled : True
P3 : Built-in
P4 : account
P5 : for
P6 : administering
P7 : the
P8 : computer/domain
True
要获得第二个结果,必须考虑标题和分隔符行。
这将返回完整的对象
((Get-Content .\test.txt) | ConvertFrom-String -PropertyNames Name, Enabled)[3]
结果:
Name Enabled P3 P4
---- ------- -- --
Name Enabled Description
---- ------- -----------
administrator1 True
Azureuser True Built-in account
DefaultAccount False A user
Guest False Built-in account
Name : Azureuser
Enabled : True
P3 : Built-in
P4 : account
P5 : for
P6 : administering
P7 : the
P8 : computer/domain
True
这将仅返回“已启用”的值
(((Get-Content .\test.txt) | ConvertFrom-String -PropertyNames Name, Enabled)[3]).Enabled
结果:
Name Enabled P3 P4
---- ------- -- --
Name Enabled Description
---- ------- -----------
administrator1 True
Azureuser True Built-in account
DefaultAccount False A user
Guest False Built-in account
Name : Azureuser
Enabled : True
P3 : Built-in
P4 : account
P5 : for
P6 : administering
P7 : the
P8 : computer/domain
True
解析这种格式并不容易,但下面是一个尝试:
$lineNo = 0
Get-Content .\test.txt | foreach {
# if first line, make a regex for parsing based on the headers
# (assumes header names have no spaces)
if ($lineNo -eq 0) {
$matches = [regex]::Matches($_, '\w+ +')
$headers = $matches | foreach {$_.Value.Trim()}
# build a regex based on column names & lengths
$regex = $(for($i = 0; $i -lt $matches.Count; $i++) {
if ($i -lt $matches.Count - 1) {
$quantifier = "{" + $matches[$i].Length + "}"
} else { $quantifier = "+" }
"(?<$($headers[$i])>.$quantifier)"
}) -join ""
}
# skip 2nd line and parse all others with the regex created before
elseif ($lineNo -gt 1) {
if ($_ -match $regex) {
[pscustomobject]$matches | select $headers
}
}
$lineNo++
# Now you can easily get the value you want
} | select Name
这将适用于所有3列。即使值中有空格。
如果标题名称中有空格,脚本将中断,但通常情况并非如此。最好再次将文本转换为具有属性的对象 这很容易,因为前两列的值中没有空格, 所以这一行:
gc .\file.txt|?{$_ -notmatch '^[ -]+$'}|%{$_.trim(' ') -split ' +',3 -join ','}|convertfrom-csv
产生以下输出:
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
看起来眼熟吗
是,但存储在变量中,即$Data=。。。,
允许您直接访问以零为基础的第二个名称索引
> $Data[1].Name
Azureuser
> $Data[2].Description
A user account managed by the system.
为了更好地解释脚本的作用,这里提供了一个dealiased版本:
$Data = Get-Content .\file.txt | Where-Object {$_ -notmatch '^[ -]+$'} |
ForEach-Object{
$_.trim(' ') -split ' +',3 -join ','
} | ConvertFrom-Csv
其中,对象{$\-notmatch'^[-]+$}删除仅包含虚线和空格的行
$\.trim“”从线条中删除trraily空格
-拆分“+”,3将任意数量大于1的空格处的线拆分为3段
它们与-join“,”组合在一起,形成一个带有头的有效csv文件
最后一个| ConvertFrom Csv完成了这项工作。这似乎是PSObject集合的文本表输出。它不是用来保存到文本文件并对其进行解析的。使用对象本身。我想从启用结果名称的描述中删除这两行。这怎么可能呢?那么我怎么才能得到描述的值呢?我喜欢这个解决方案!非常苗条,适用于给定的示例+1,但不幸的是容易中断,最简单的情况是名称中的空格或描述中的逗号。