powershell remove first characters of line

10,566

Solution 1

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

Solution 2

Assuming the log line is in a variable of type string this should do it:

$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)

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

Share:
10,566
Bluz
Author by

Bluz

Updated on November 21, 2022

Comments

  • Bluz
    Bluz over 1 year

    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..

    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.

    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.

    Any help would be greatly appreciated,

    Thanks a lot!