PowerShell: Read text, regex sort, write output to file and formatting

Posted by Bill Hunter on Stack Overflow See other posts from Stack Overflow or by Bill Hunter
Published on 2010-05-26T23:22:40Z Indexed on 2010/05/26 23:41 UTC
Read the original article Hit count: 270

Filed under:
|
|
|

I am a Powershell novice and have run into a challenge in reading, sorting, and outputting a csv file. The input csv has no headers, the data is as follows:

05/25/2010,18:48:33,Stop,a1usak,10.128.212.212
05/25/2010,18:48:36,Start,q2uhal,10.136.198.231
05/25/2010,18:48:09,Stop,s0upxb,10.136.198.231

I use the following piping construct to read the file, sort and output to a file:

(Get-Content d:\vpnData\u62gvpn2.csv) | %{,[regex]::Split($, ",")} | sort @{Expression={$[3]}},@{Expression={$_[1]}} | out-file d:\vpnData\u62gvpn3.csv

The new file is written with the following format:

05/25/2010
07:41:57
Stop
a0uaar
10.128.196.160
05/25/2010
12:24:24
Start
a0uaar
10.136.199.51
05/25/2010
20:00:56
Stop
a0uaar
10.136.199.51

What I would like to see in the output file is a similar format to the original input file with comma dilimiters:

05/25/2010,07:41:57,Stop,a0uaar,10.128.196.160
05/25/2010,12:24:24,Start,a0uaar,10.136.199.51
05/25/2010,20:00:56,Stop,a0uaar,10.136.199.51

But I can't quite seem to get there. I'm almost of the mind that I'll have to write another segment to read the newly produced file and reset its contents to the preferred format for further processing.

Thoughts?

© Stack Overflow or respective owner

Related posts about powershell

Related posts about sort