【问题标题】:powershell remove first characters of linepowershell删除行的第一个字符
【发布时间】:2012-01-14 01:24:20
【问题描述】:

我正在使用 powershell 解析 JBOSS 日志文件。 典型的行是这样的: 2011-12-08 09:01:07,636 错误 [org.apache.catalina.core.ContainerBase.[jboss.web].etc..

我想删除字符 1 中的所有字符,直到单词 ERROR。所以我想删除日期和时间、昏迷和紧随其后的数字。我希望我的行以 ERROR 开头并删除之前的所有内容。

我查看了谷歌并尝试了我发现的不同的东西,但我很挣扎并且无法让它工作。我尝试使用子字符串并替换,但直到 ERROR 一词才找到如何删除所有字符。

任何帮助将不胜感激,

非常感谢!

【问题讨论】:

    标签: powershell line character


    【解决方案1】:

    这一行将读取文件的内容(在示例 jboss.txt 中)并将包含 ERROR 的每一行替换为 ERROR + 后面的任何内容 .最后,它会将结果保存在processed_jboss.txt中

    get-content jboss.txt | foreach-object {$_ -replace "^.*?(ERROR.*)",'$1'} | out-file processed_jboss.txt
    

    【讨论】:

      【解决方案2】:

      假设日志行位于字符串类型的变量中,应该这样做:

      $line = "2011-12-08 09:01:07,636 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].etc.."
      
      $ErrorIndex =  $line.indexof("Error",0)
      $CleanLogLine = $Line.Substring($ErrorIndex, $line.length)
      

      参考: http://msdn.microsoft.com/en-us/library/system.string.aspx

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-24
        • 2022-08-10
        • 2020-11-17
        • 2011-05-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多