View Single Post
Old 07-20-2007   #1 (permalink)
Cornelius


 
 

How do I read a text file and sort text by fixed positions?

I need to read the content of a file and sort the content by fixed width.
Example file: sample_data.txt
23abdc1133cdde
14zzwd0034kkhe
65ppok7780hyyh

Read in the content and sort text by fixed position
(startpos:1,len:2)(startpos:3,len:4)(startpos:6,len:4)

I'm having problems sorting the text after i read the file. I think i need
to use regex and sort the matches object.

This is what i have so far:

Get-Content c:\text_acct_level.txt | ForEach-Object { if ($_ -match
[regex]"^.{2}(?<fld1>(.{9}))(?<fld2>(.{4}))(?<fld3>(.{20})).*"){$matches}}|sort-Object $matches[1],$matches[2],$matches[3]

Its not working but at maybe it will help you understand where I'm getting
lost.

Thanks,
Cornelius

My System SpecsSystem Spec