View Single Post
Old 07-20-2007   #2 (permalink)
Jacques Barathon [MS]


 
 

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

"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 am lost with your question: if you want to sort the file by the first two
characters then the next four characters and finally the last four
characters, won't sorting the whole line by default produce the expected
result?

PS> get-content sample_data.txt | sort

However, if you want to sort on non-sequential patterns of your text the
following should work:-

--- sample_data.txt ---
23abdc1133cdde
14zzwd0034kkhe
65ppok7780hyyh
12abdc1133cdde
--- sample_data.txt ---

PS> # sort on 3rd to 6th chars, then on 1st to 2nd chars:
PS> get-content sample_data.txt | sort {$_[2..5]},{$_[0..1]}
12abdc1133cdde
23abdc1133cdde
65ppok7780hyyh
14zzwd0034kkhe

Hope that helps,
Jacques

My System SpecsSystem Spec