View Single Post
Old 07-20-2007   #4 (permalink)
/\\/\\o\\/\\/ [MVP]


 
 

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

You can also use ADO the read fixed length delimited files, by using a
schema.ini (can be generated in controlpanel)

I describe this method in this entry on my old blog :

http://mow001.blogspot.com/2006/07/w...delimited.html

Greetings /\/\o\/\/
http://thePowerShellGuy.com

"Cornelius" <Cornelius@discussions.microsoft.com> wrote in message
news:CC9895DF-ED1B-4EAF-8421-CB78157DA9F7@microsoft.com...
> Sorry I know the post was a mess and not very clear. I'm trying to sort
> non-sequential
>
> It worked great! Thank you.
> Powershell is so much fun when you know what you are doing. It looks like
> i
> need more practice...
>
> "Jacques Barathon [MS]" wrote:
>
>> "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