【发布时间】:2015-06-03 20:24:09
【问题描述】:
我有一个脚本,它读取一个文本文件,通过 select-string 捕获特定的字符串值,然后写入一个 csv 文件。我遇到的问题是它会自动添加我不想要的其他列:
IgnoreCase LineNumber 行文件名路径模式上下文匹配
我想要的字符串值在 Line 下 - 如何让它只输出我捕获的字符串值?
【问题讨论】:
标签: powershell
我有一个脚本,它读取一个文本文件,通过 select-string 捕获特定的字符串值,然后写入一个 csv 文件。我遇到的问题是它会自动添加我不想要的其他列:
IgnoreCase LineNumber 行文件名路径模式上下文匹配
我想要的字符串值在 Line 下 - 如何让它只输出我捕获的字符串值?
【问题讨论】:
标签: powershell
使用Select-Object -ExpandProperty 从Select-String 返回的MatchInfo 对象中获取单个属性值:
$MatchedStrings = Get-Item C:\Windows\system32\WindowsPowerShell\v1.0\en-US\about_Foreach.help.txt |select-string "operator"
$MatchedStrings | Select-Object -ExpandProperty Line | Out-File C:\myStrings.txt
如果要输出到单列 CSV 文件并保留 Line 标头,请改用 Select-Object -Property:
$MatchedStrings | Select-Object -Property Line | Export-Csv C:\strings.csv -NoTypeInformation
【讨论】: