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


 
 

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

Bruce its cool to see your reply, being that I'm on Chapter 5 of your book. I
guess I should have figured it out but, for some reason I just didn't think
about the get-content sending an array object. I'm learning so much so fast,
I have to use it daily to retain most of what I'm learning.

Thanks for writing a great book.

Sincerely,
Cornelius

"Bruce Payette [MSFT]" wrote:

> You can specify a scriptblock as an argument to the sort command that will
> be used to calculate the value to sort on. For example:
>
> PS (23) > $text.split("`n") | sort # default sort
> 14zzwd0034kkhe
> 23abdc1133cdde
> 65ppok7780hyyh
> PS (24) > $text.split("`n") | sort {[int] ($_.substring(1,1) +
> $_.substring(6,4)) } # numeric sort on 2nd char + middle num string
> 23abdc1133cdde
> 14zzwd0034kkhe
> 65ppok7780hyyh
> PS (25) >
>
> -bruce
>
> --
> Bruce Payette [MSFT]
> Windows PowerShell Technical Lead
> Microsoft Corporation
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
>
> "Cornelius" <Cornelius@discussions.microsoft.com> wrote in message
> news:1544EB07-3A08-47EE-BB53-0DF3E2C12074@microsoft.com...
> >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