【问题标题】:Why wont this powershell script execute为什么这个powershell脚本不执行
【发布时间】:2016-02-02 14:40:08
【问题描述】:

我正在尝试制作一个执行以下操作的脚本:

  1. 打印文件
  2. 删除所有空行
  3. 过滤掉所有不包含“重新分配”一词的行
  4. 从每行的开头删除所有空格和“>”

在我为第 4 步添加函数之前,一切正常,之后 powershell 会吐出一堆错误。我在这里做错了什么?

PS C:\Users\pzsr7z.000\Desktop\incidentoutput> cat .\csvtest.txt | ? {$_.trim() -ne "" } | sls "^Reassignment$" -Context 1,2 | foreach{ $_.TrimStart(">"," ")}
Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo] does not contain a method named 'TrimStart'.
At line:1 char:90
+ ... 1,2 | foreach{ $_.TrimStart(">"," ")}
+                    ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo] does not contain a method named 'TrimStart'.
At line:1 char:90
+ ... 1,2 | foreach{ $_.TrimStart(">"," ")}
+                    ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo] does not contain a method named 'TrimStart'.
At line:1 char:90
+ ... 1,2 | foreach{ $_.TrimStart(">"," ")}
+                    ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo] does not contain a method named 'TrimStart'.
At line:1 char:90
+ ... 1,2 | foreach{ $_.TrimStart(">"," ")}
+                    ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

所有功能都单独工作,当我尝试添加最后一部分时,事情就搞砸了。

【问题讨论】:

  • 不包含... ???什么?

标签: windows powershell


【解决方案1】:

我的通灵能力告诉我,你正试图对Select-String cmdlet 返回的MatchInfo 对象使用StringTrimStart 方法。

先尝试将其转换为字符串:

... | sls "^Reassignment$" -Context 1,2 | foreach{ $_.ToString().TrimStart(">"," ")

【讨论】:

  • 您好,当我尝试使用它时,trim 函数似乎只处理匹配输出的第一行(参见屏幕截图链接)imgur.com/GHggNEs,而不是所有行。什么会导致它这样做?
  • @lacrosse1991 好吧,我不知道为什么它对你有用。您正在读取数组的内容(每一行都是数组的一个元素),然后将每个元素与Select-String 匹配。但这意味着Select-String 没有返回的上下文,因为它一次只能看到一行。你能提供一个csvtest.txt 文件的例子吗?和你的previous question一样吗?
  • 您好,很抱歉花了这么长时间才回复。尽管我采取了不同的路线,但我最终还是让它工作了。你觉得这个脚本怎么样?我是 powershell 的新手(昨天才开始,之前只做过 perl、bash 和一些 python,bash 是我最喜欢的)所以我很感激你的任何意见。我刚刚截取了屏幕截图,因为 pastebin/textsharing 站点在网络上被阻止。脚本:imgur.com/wD2cXxI 输入:imgur.com/JNTpSTx(来自大型文本文件的文本样本,我正在同时处理多个文件)输出:imgur.com/C9J4IDl
  • @lacrosse1991 有一个专门的 SE 站点可以解决这类问题:codereview.stackexchange.com,在那里讨论您的代码会容易得多。快速浏览一下,您不需要临时文件,老实说,previous question 的解决方案应该可以正常工作,您只需将其扩展为去除空格和>。就我个人而言,我只是使用regex(将\n 替换为\r\n)来获取所有数据并在使用Export-CSV 保存到CSV 之前对其进行修剪。
【解决方案2】:

此正则表达式模式:^Reassignment$ 将仅匹配具有 精确Reassignment 的行 - 并非所有 包含 Reassignment 的行。

当您已经使用Get-Content (cat) 从文件中读取行时,您也可以在所有行上使用-match 运算符(-cmatch 区分大小写):

(cat .\csvtest.txt) -cmatch 'Reassignment'

-match 运算符只返回匹配的字符串,因此您可以直接在这些字符串上调用TrimStart()

$ReassignmentLines = (cat .\csvtest.txt) -cmatch 'Reassignment' |ForEach-Object {
    $_.TrimStart(">"," ")
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多