【问题标题】:Powershell: Only export string valuesPowershell:仅导出字符串值
【发布时间】:2015-06-03 20:24:09
【问题描述】:

我有一个脚本,它读取一个文本文件,通过 select-string 捕获特定的字符串值,然后写入一个 csv 文件。我遇到的问题是它会自动添加我不想要的其他列:

IgnoreCase LineNumber 行文件名路径模式上下文匹配

我想要的字符串值在 Line 下 - 如何让它只输出我捕获的字符串值?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    使用Select-Object -ExpandPropertySelect-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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      • 2020-06-04
      • 2019-01-24
      • 2021-07-28
      • 2021-05-11
      • 1970-01-01
      相关资源
      最近更新 更多