powershell删除行的第一个字符 [英] powershell remove first characters of line

查看:483
本文介绍了powershell删除行的第一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am parsing a JBOSS log file using powershell. A typical line would being like this : 2011-12-08 09:01:07,636 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].etc..

我想删除字符1中的所有字符,直到出现ERROR这个词.所以我想删除日期和时间,逗号和数字.我希望我的行以ERROR一词开头,然后删除之前的所有内容.

I want to remove all the characters from character 1 until the word ERROR. So I want to remove the date and time, the coma and the number right after it. I want my lines to begin with the word ERROR and delete everything before that.

我在Google上进行了尝试,并尝试了发现的不同方法,但是我很挣扎,无法使其正常工作.我尝试使用子字符串进行替换,但是直到出现ERROR字样之前,我都找不到删除所有字符的方法.

I looked on google and tried different things I have found but I struggle and can't make it work. I tried with substring and replace but can't find how to delete all characters until the word ERROR.

任何帮助将不胜感激,

非常感谢!

推荐答案

此单行代码将读取文件的内容(在示例jboss.txt中),并替换每一行该行中包含ERROR + ERROR的错误.最后,它将将结果保存到处理过的_jboss.txt

This one-liner will read the contents of your file (in the example jboss.txt) and replace every line containing ERROR by ERROR + whatever follows on that line. Finally it will save the result in processed_jboss.txt

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

这篇关于powershell删除行的第一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆