vjpandian
vjpandian

Reputation: 604

Formatting a Log file in PowerShell or Batch

I have a log file that looks that looks like the example below:

[hh:mm:ss] LINE 1 GOES HERE
   LINE 2 GOES HERE
   LINE 3 GOES HERE
   LINE 4 GOES HERE
[hh:mm:ss] LINE 5 IS HERE

I'm looking for a way to format this file somewhat like this

[hh:mm:ss] LINE 1 GOES HERE LINE 2 GOES HERE LINE 3 GOES HERE LINE 4 GOES HERE
[hh:mm:ss] LINE 5 GOES HERE 

Is there any easy way to do this in PowerShell or CMD.exe ? Any help is appreciated!

Here's what I've come up with so far:

$path = "---path to file----"
Get-Content "$path" | Where { $_.trim() -ne ""} | Set-Content "$path"

This only moves lines up if there is an empty line above, but doesn't really move empty lines behind the original line.

Upvotes: 2

Views: 113

Answers (1)

Nick Schroeder
Nick Schroeder

Reputation: 1462

EDIT: Updated based on your example code.

I pasted your example into log.log and did this in Powershell:

((Get-Content "$PSScriptRoot/log.log") -join "`n").Replace("`n"," ").Replace("[","`n[") | Set-Content "$PSScriptRoot/log.log"

Upvotes: 3

Related Questions